In this basic "Hello World" style of article, I will create a repository in Microsoft Azure, clone it, create a new C# project in VS Code and run the whole thing. If you have already done anything like this before, there is not much to learn here. Also the difference between cloning a repository from Azure or other sources like GitHub is negligible - they all work sort of similar.
Azure repos
Let's start with creating a remote repository to hold our code. In this article I'm using Microsoft Azure repos, but obviously other options like GitHub, GitLab and many more also works fine. Login to your https://dev.azure.com account, click on (or create) the project you want the repository to be created in, and then go to "Repos". In the top menu, click the name of the repo (or project if there are no other repos available) and select the "+ New repository".
In the "Create a repository" popup, select Git, select a name for the repo and click "Create" in the bottom right corner.
Thats it! A new repository is created and there should be a template README.md file in it.
Clone with Git
The next step is to clone the repository from Azure to the local computer where Git and VS Code is installed. I have already created a folder "C:\dev\repos" where my repositories will be stored. Navigate to this folder and right-click in it and select "Git Bash Here". (This can all be done within VS Code as well, just giving an extra option here)
To clone the repo, we need it's URL. Go back into Azure and click the "Clone" button in the top right corner while you have selected the repository that you want to clone. This will show the complete URL and you can simply copy it and then issue the command:
git clone [the copied URL]
You might get a prompt to login, but other than that it should be everything needed to have created a local clone of the Azure repository. Check if the folder exists by running the command "ll" and it should now look something like this.
Navigate to the repository by running the command
cd HelloWorldCSharp
Note how the prompt changes slightly and you can now see what branch you are currently on (main). Next - open up VS Code by the shorthand command
code .
The final thing we need to do is to add a .gitignore file. We are going to create a C# project and there will be a lot of temporary files generated everytime the we build the project. To avoid any confusion with these files, we should tell Git that it does not have to bother with any of them - just ignore them. And thankfully there are ready-made gitignore files for pretty much any project type out there. This one I downloaded from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore. Make sure to rename the file ".gitignore" and place it directly in the root of the repository.
VS Code and C#
With all the work above completed, it's finally time to create the C# project and run it. Before we can do that however, we have to make sure we have the C# Dev Kit and SDK installed. In VS Code, click the extensions button and search for "C#". You will probably find the official C# Dev Kit from Microsoft as one of the first extensions. Click the install button and wait. In order for this to work though, we also need the .NET SDK installed. This can be found at https://dotnet.microsoft.com/en-us/download. I choosed the .NET 6.0 LTS version for this one. Simply download the file, run it and wait until it has been installed.
Run the command
dotnet
in a prompt (or git bash terminal in VS code). If you get "bash: dotnet: command not found" then something has not worked (if it is correctly installed you will see a bunch of options instead). Sometimes a simple restart of VS Code or the terminal that you are using is enough. If not - try to restart the computer.
Next step is to create the C# project. Simply run the command:
dotnet new console --framework net6.0 --use-program-main
This will create the project file (HelloWorldCSharp.proj) and the main program starting function (Program.cs). In VS Code, select the Program.cs file. This might trigger VS Code to want to add a couple of asssets needed for debugging - if so, let it do that. Also - since this is the first time we open up a C# file on this computer, there might be a few extra updates that VS Code suggests that could be useful.
In the "Program.cs" file, you will find the actual code. When creating a project like this we don't even have to write the iconic Hello World ourself since it is already made for us.
namespace HelloWorldCSharp;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Run the command
dotnet run
in the git bash terminal and you should see the text "Hello World" written to the console.
Push changes to Azure
Now that we have a running program, we should save the changes and copy them back to the Azure repository. This can all be done via command-line, but I'm showing VS Code version here. In VS Code, click the "Source Control" button (shortcut: CTRL+SHIFT+G).
Normally you should not commit directly to master/main branch, but sometimes it just makes sense to take a shortcut :)
Write a message in the box, select the "Stage All Changes" button and then click the "Commit" button. If this is the first time committing anything in Git, you might get an error like the one below.
The error is easy to fix though - simply run the following commands (replace myemail@email.com and MyName with your actual email and name used to login to Azure):
git config --global user.email "myemail@email.com"
git config --global user.name "MyName"
Next - push the changes to Azure repos. Either use the "Push" button (hidden behind the 3 dots in the top corner) or "Sync Changes". This might trigger a Git Credentials login (use Azure user/password)
And thats it - the changes should now have been pushed to the origin (Azure). Login and make sure that is the case.
Conclusion
You are right - it's not very difficult at all. Perhaps a bit confusing at first, but it all comes together in the end. Almost every step I show here have alternatives and at some point you will probably find one that suits your style better. But I hope you enjoyed a little bit of ranting and that it wasn't too chaotic. :)
This code is available on my public Azure Repos at https://dev.azure.com/drygast/public/_git/HelloWorldCSharp