Building a C# console application with Visual Studio Code

Overview

If you are like me, used to working with Visual Studio, you could feel like something is missing when trying to create a new .Net project in Visual Studio Code. Where is my File -> New Project?

In this article we’ll see how to create a new C# console application using the .NET Core Command Line Tools.

You will need

And nothing else! No npm, no Yeoman and no MSBuild.

Let’s do it!

The .Net Core CLI provides all the functionality needed to scaffold, build, run, test and deploy a .Net Core application.

The File -> New Project functionality will be done using the command:

dotnet new -t console

At this point you might want to look at what was generated in VS Code.

code .

We now have two files:

  • Program.cs
  • project.json (the .Net Core project file, for now…)

In order to resolve the dependencies of the project.json file we run:

dotnet restore

This will create a project.lock.json with the project packages. You can read about it here.

And now we are ready to run our console application.

dotnet run
At this point you might want to add an extension to VS Code to work with C# with Syntax Highlighting, Intellisense and debugging support. Go to Extensions and install C# for Visual Studio Code (powered by OmniSharp) and then Enable it.

If we now open a .cs file in VS Code we should get a message asking if we want to add the required assets to build and debug the project. This will add two new files under the .vscode folder.

  • launch.json
  • tasks.json

We are now ready to debug our console application. Add a breakpoint to the Program class and press F5. This should switch the view to the debug mode where you can see the output, variables, stack, etc.

The code can be found here.

That’s it for today. On the next post I’ll cover test and deployment.

Please comment if you have any question or if you find this useful.