Visual Studio Code for Software Development (Douglas Starnes, may 2020)
In the past few years, Visual Studio Code has gained enormous popularity and credibility as a first class development tool. Though the name might suggest a tight coupling with Microsoft technologies, Visual Studio Code is not Visual Studio “Lite”. As Microsoft moved along the platform independent road with it’s release of .Net Core, it also made sense to become tool independent. That’s where Visual Studio Code comes in. Visual Studio Code is free, cross platform, and open source. To download Visual Studio Code, go to https://code.visualstudio.com/.
The default installation of Visual Studio Code does not provide support for .NET development (only through extensions). In fact, for specific .Net development I would still recommend Visual Studio. Think of Azure Funtions and Azure App Services. Personally, I use Visual Studio Code for developing YAML pipelines and ARM templates. Three interesting extensions are:
- Azure Resource Manager Tools. For developing ARM template files.
- Azure API Management. For generating ARM templates for API Managament services. Formerly only possible via the separate API Management Template Generator tool.
- Azure Logic Apps. For generating ARM templates for Logic Apps. Formerly only possible via the separate Logic App Template Generator tool.
Visual Studio Code starts off with just a minimalist experience. Out of the box it’s just a text editor. On the left-hand side there’s a toolbar with a File Explorer at the top, a Source Control button and an Azure Explorer Window.
Visual Studio Code comes out of the box with integration for Git. If the directory open in the Explorer is a Git repository, Visual Studio Code will recognize it as such and you can track changes, make commits and more from the Source Control pane (Ctrl-Shift-G). The icon for the Source Control pane shows the number of changes. If the directory is not yet linked to Git, you can hit the Initialize Repository button.
The U means that the status of the files is Untracked. Add a commit message and click Ctrl-Enter. Now you can push and sync, but also pull or create a branch.
Via the View menu, you can also open the Command Palette (Ctrl-Shift-P) or the Command-Line Window (= Terminal Window, Ctrl-`). Using menu File / Open Folder would be easier, but as an example. To open a folder in the Explorer window via the Command-Line Window:
- Navigate to directory you want to open (cd ..)
- Type command: $ code -r .
- The code command will start VS Code from the command line.
- The -r option will reuse the existing instance (no new one)
- The . is the path to the directory. You could also pass a relative path
- Next you can add subdirectories and files to the directory.
Now let’s look at the situation where you want to develop in .Net. Search for extension “C#”. Install C#. Also install C# Extensions. This adds a few shortcuts. Visual Studio Code does not have a fancy user interface like Visual Studio does. So, from the command line, run: $ dotnet new console -o DemoApp
This will create a new .NET Core console project named DemoApp in a new directory called DemoApp. Restart Visual Studio Code and open the DemoApp project: $ code -r DemoApp
Visual Studio Code will first show a notification that a few required assets for build and debug are missing. Click [Yes] and the necessary metadata will be added to the project in the .vscode directory. The notification will disappear and you can open the command palette. Searh for command .NET: Generate Assets for Build and Debug. Now switch to the Debug pane using Ctrl-Shift-D. Select from a dropdown at the top either .Net Core Launch or .Net Core Attach. The .NET Core Launch option will launch the app in debug mode.
- Right-click in the Explorer pane and select New Class.
- In the Command Palette, enter filename, ie User.cs.
- This will scaffold a class named User in an appropriate namespace.
- Generate properties just like in Visual Studio. Type the snippet prop and press the tab key.
- Press Ctrl-P and name the file Program.cs. Implement the Main method.
- When you use .Net classes like StringBuilder, you can click Ctrl-. to select the correct namespace (System.Text).
- To work with NuGet packages, install extension NuGet Gallery.
- To select a NUGet package, open the Command Palette and search for “NuGet: Open Gallery”. You will see an interface similar to Visual Studio.