Getting Started With C#, .NET Core And VSCode is quite different from Visual Studio and the full .NET framework. In this post I’m going to share what I learnt to get you up and running quickly with your first .NET Core C# console application on Windows.
What to install
Apart from VSCode you also need the .NET Core SDK which installs the .NET Core CLI tools required by VSCode, you can find the SDK here. In VSCode you have to install the C# extension.
Create A New C# Project
You can’t import solutions from Visual Studio into VSCode, best way seems to be creating a new project and adding the .cs files you need. To keep things organised create a new empty folder to store the project. In VSCode open the empty folder
and run
dotnet new
in the terminal to create a new console application in the folder. You will see a program.cs and {projectname}.csproj file. VSCode will warn you that additional assets are required to build and debug the application, select yes to install them or else the build and debug tasks won’t work later on. You can see a list of templates available by running
dotnet new -all
Build And Run A C# Project
To build your project select Run Build Task from the Tasks menu and select Build in the next dropdown.
You can also build your project from the command line by running
dotnet restore dotnet build
Debugging is pretty much self explanatory select it from the Debug menu or press F5. To run your application without debugging run
dotnet run
in the terminal.
Adding .NET Core References And Packages
VSCode is a handy little editor but not really meant for large projects, that said if you really want to use it for a non-trivial application you will probably split the solution into multiple projects and organise your code in folders.
To add source files contained in sub folders to your project select Add Folder to Workspace.
Adding references to your project basically entails editing the .csproj file but there are dotnet commands or extensions to make it easier
To add a project reference run
dotnet add {yourproject.csproj} reference otherproject.csproj
The path to your project is optional if the project is in the current directory.
To add nuget packages run
dotnet add package PackageName
Alternatively install the Nuget Package Manager extension and use the Command Palette.
To reference a DLL install the Add Local .NET Reference extension and use the Command Palette.
.NET Core itself is broken up into packages that you add as you need them. For example if you need Linq run
dotnet add package System.Linq
There are also Metapackages that contain groups of related packages, more here.
VSCode and .NET Core is still evolving and some tasks can only be accomplished using the CLI tools the CLI reference can be found here.