Using VSCode with Dotnet Core

Lightweight Cousin

Hopefully, you read up on my previous post about using and installing dotnet core and went over some of the reasons why you might want to use it for your next project. Especially if you are coming from the .NET world and have not yet had the opportunity to check out the lightweight cousin of the .NET framework.

Just the Good Parts

Using dotnet core allows you to only use the parts of the framework that you need without having to install the entire .NET framework, as well as being cross-platform, opensource and running on top of CoreCLR (Which includes garbage collection and JIT Compiler). So if you are needing to meet any of these requirements or have an interest in the direction of where Microsoft is taking things, I really recommend you checkout dotnet core. You can read my previous post about getting setup and installing the dotnet core framework.

Why VSCode over Visual Studio?

You can absolutely use Visual Studio (If you go this route, you’ll need to use 2015 and above) which as a full-blown IDE in many cases will still make sense. If you are used to the full-blown features of Visual Studio then this may be your default, however, I’ve grown to love the lightweight and extensible feel of VSCode, and if used correctly in most cases should be all that you need.

Not an IDE

I would not consider VSCode to be an IDE, so much as an awesome text editor with extensions that allow you the ability to build, run and debug your code. I think I myself even call it an IDE from time to time, but this is not the case, it is really just an awesome text editor with tons of extensions. I love using VSCode for web development, as there are some nice code linter’s and a variety of extensions available, as well as it is extremely flexible in customizing to your own preferred experience (themes, layout, hotkeys, etc). As I said, this may not be your default for development, especially if you are used to Visual Studio, but you really ought to give it a try.

C# Extension for VSCode

If you are planning on using VSCode to develop against dotnet core, then you will definitely want the C# extension. To install simply open VSCode, and using either ctrl + shft + x or hitting the Extensions on the sidebar and type in C#, look for the one supported by Microsoft. Click install. From the detailed description on the installer, the extension offers the following features.

  • Lightweight development tools for .NET Core.
  • Great C# editing support, including Syntax Highlighting, IntelliSense, Go to Definition, Find All References, etc.
  • Debugging support for .NET Core (CoreCLR). NOTE: Mono debugging is not supported. Desktop CLR debugging has limited support.
  • Support for project.json and csproj projects on Windows, macOS and Linux.

There are a variety of other extensions available as well, so you may want to check them out. For example, there is an IL Viewer that looks extremely useful as well as a code snippets extension. Please comment below about any extensions that you find useful which make your development experience more enjoyable.

Using the dotnet core 2.1 CLI

Yes, that is right, dotnet core comes complete with a command line interface to help in your development experience. I am doing this demonstration using dotnet core 2.1. I’d recommend staying away from 1.0 unless you are working on some older dotnet core applications. You can check your version of dotnet core by running in a terminal dotnet --version. With the CLI there are a variety of commands, and which will help greatly with your development experience when creating and building a dotnet core app. For this example, we are just going to run through a brief tutorial in setting up a basic console app, and I’ll further show you how to use debug along the way using VSCode.

dotnet new console

In VSCode, create a new folder and open it in your editor. Open up a terminal or cmd prompt by typing type ctrl + `. This will open up a terminal window below for you, that way you can have your terminal open in VSCode, and enter dotnet new console. This will autogenerate your code for you and create a simple console application. Note that this will also run the dotnet restore command, which will install your project dependencies, including any nuget packages if the project had any at this point. In dotnet core 1.0 you used to have to run the dotnet build command and then run the dotnet run command, but as of core 2.0, you can just use the dotnet run command and this will build and run your application. If all goes well, you should see “Hello World!” as an output. Success, you have just run your console app.

Using Our Extension

Nothing real fancy, but if you open up the Program.cs in our app file you’ll notice VSCode starts to run a process and you’ll get output in our editor. This is the initial setup of our C# Extension running, and installing a few dependencies to get the extension to work with our .cs code files.

Installing C# dependencies...
Platform: win32, x86_64
Downloading package 'OmniSharp for Windows (.NET 4.6 / x64)' (31021 KB).................... Done!
Installing package 'OmniSharp for Windows (.NET 4.6 / x64)'
Downloading package '.NET Core Debugger (Windows / x64)' (43046 KB).................... Done!
Installing package '.NET Core Debugger (Windows / x64)'
Downloading package 'Razor Language Server (Windows / x64)' (46894 KB).................... Done!
Installing package 'Razor Language Server (Windows / x64)'

Now if you hit ctrl + shft + d you’ll open up the debugger window. You can also do the same by clicking on the bug icon in the sidebar. You’ll notice that I’ve already set a breakpoint and edited the code a bit to return my name in the console output.

Here is the code for you if you want to copy pasta…

using System;
namespace dotnet_core_setup
    class Program
        static void Main(string[] args)
            string myName = "Nick";
            Console.WriteLine($"Hello {myName}"); 

With your debugger console open you can see the green arrow and a dropdown. The dropdown allows you to attach to a process, or if you need to will allow you to create your own configuration for when things get complicated, but for now, you can just run this under the default of the console. You’ll notice that there are options in your debugger console for variables, watches, call stack, and viewing your breakpoints. If all goes well, you should see your breakpoint be highlighted and your watches or your variables come to life, as well as a small menu for your debugging options (note the use of your shft and f5, f10, f11 keys), for stepping/continue and stopping your code. In my opinion, this is just about all you should need for a comprehensive yet minimalist debugging experience. Don’t get me wrong, Visual Studio 2015-2017 offers some additional tools that are just not present in VSCode (code metrics/analytics, etc), however for much of what we as developers need to do, I think VSCode is excellent. I hope you found this useful and please don’t forget to comment on your thoughts. I love to hear them.

Leave a Reply

Your email address will not be published. Required fields are marked *