Framework and Prerequisites

Before we can start writing C# code on our machine, there are a few packages we need to install as prerequisites.

With these software packages installed you should be good to go. Your system is now ready to start writing, building and running C# code with any code editor. Although this guide is focused around using Visual Studio Code, please skip to the next section if you'd prefer to use a different code editor.

Setting up Visual Studio Code

Download the latest version of VsCode here and follow the installation instructions for your Linux distribution. If you are running a Debian/Ubuntu based Linux distro, download the *.deb installer and install VsCode with the dpkg command.

sudo dpkg -i <VsCodeFileName>.deb  

Once you've installed VsCode, open it up and install the C# extension by Omnisharp which will provide intellisense, autocomplete and other helpful notifications in your editor.

Creating and Running C# Projects

Creating a new .Net Core project is easy and can be done from the .Net Core command line interface using the "dotnet new" command followed by the type of project you want to create. For this example we will be creating a simple console app but you can find the documentation about the .Net Core CLI https://docs.microsoft.com/en-us/dotnet/articles/core/tools/.

To create a new project, first of all create a directory for the code to sit and then use the dotnet new command.

mkdir myconsoleapp  
cd myconsoleapp  
dotnet new console  

In order to build/run the project we need to download the project dependencies using the "restore" command, build it with the "build" command and run it with the "run" command.

dotnet restore  
dotnet build  
dotnet run  

When running the default C# console project, the program should display a simple "Hello World!" message.

We can dive in to VsCode and look at the code of our current working directory using the "code" command followed by a full stop (period).

code .  

The dotnet cli should've created a myconsoleapp.csproj project file and a Program.cs code file containing the following code.

using System;

namespace myconsoleapp  
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Hello World!");
        }
    }
}

In VsCode you should be able to edit the code, save the file then build and run it via the command line. You may even build and run the code inside VsCode using the integrated terminal (View > Integrated Terminal)

Installing Nuget Packages

An important aspect of .Net Development is being able to make use of third party tools and packages available via Nuget.

For those who have used earlier versions of the .Net Core framework this used to be easy as new projects used to contain a project.json file which has a list of all the Nuget Packages/Dependencies in a project which could be edited easily with full intellisense in VsCode.

Sadly this isn't the case now, following the version 1 release of .Net Core, the team decided to revert back to the old XML based *.csproj based files which are an absolute pig to edit manually in an editor.

Thankfully for us there is now a Nuget Package Manager extension available for VsCode that enables us to do so. Install this extension then you'll be able to install Nuget Packages into your .Net Core projects via the command pallet in VsCode and your project file will be updated automatically.

For this example let's add the popular DateTime package NodaTime to our project using the following commands from the VsCode command Pallette.

Nuget Package Manager: Add Package  
NodaTime  

Now we should be able to reference the NodaTime package with a using statement in our code and call the library with full intellisense (thanks Omnisharp!). I've added some NodaTime code to our previous example here.

using System;  
using NodaTime;

namespace myconsoleapp  
{
    class Program
    {
        static void Main(string[] args)
        {
            Instant now = SystemClock.Instance.GetCurrentInstant();
            Console.WriteLine($"Hello World!, the time is {now.ToString()}");
        }
    }
}

Now if we restore, build and run our app we should get our hello message with the current date from NodaTime.

Enjoy writing C# code with your editor of choice on Linux!

Follow up topics will include debugging and unit testing .Net Core applications on Linux.