How to Create Your First .NET Application, on Linux, in 5 Minutes or less

Let’s say you wanted to become a developer for Microsoft .NET languages, such as C#. You’d have very good reasons to do so – C# is a powerful language (one might be tempted to say it’s Java done right), and .NET is a really great ecosystem.
Unfortunately, your clients demand your programs run on Linux systems – or you have chosen Linux as your primary development environment, so just installing Visual Studio is not an option for you. And since .NET applications only run on Windows, you’d be out of business within a couple of days, right?

This is where .NET Core comes into play: .NET Core is a cross-platform platform for .NET applications (try saying that three times in a row!). It’s easily installed on Unix (as well as Mac OS or Windows). For Ubuntu 16.4, run the following commands. For other platforms, check out the official download page.

Note: If you want to run this in a Docker container, you’ll want to check out the official Docker image instead.

Install the dotnet Command

sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'
apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
apt-get update -y
apt-get install -y dotnet-dev-1.0.0-preview2-003121

And that’s it! You now have the dotnet command at your disposal to create new .NET projects, build them and run them. Let’s start by creating your first C# project:

Your First C# Project

mkdir dotnet-test
cd dotnet-test
dotnet new

dotnet new will create a new project in the current directory. If no options are specified, it creates a C# Console application. You can, however, choose between C# and F# with the --lang switch, and between project types with the --type switch:

--lang Programming Language
cs Creates a new C# project
fs Creates a new F# project
--type Application Type
console Simple ‘Hello World’-type console application
web (C# only) ASP.NET Core 1.0-based web project
lib (C# only) Portable class library for use in other projects
xunittest (C# only) Unit test project with xUnit

Let’s take a look at the files that were created with the above command. There are two of them, project.json and Program.cs.

project.json is a modern form of the old *.csproj files. It is much less verbose and can be written completely manually (so no more strange GUIDs – hooray!):

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

The largest part of this file is pretty self-explanatory – and the parts that aren’t are not particularly interesting (and more interesting options can be found in the documentation). The most interesting bit, however, is the dependencies object. Here, you can define dependencies for your app in the form of key-value pairs (or, rather, library-version pairs). These dependencies will be resolved by NuGet packages once you restore your app. We will see how you do this later on.

The second file that was created by dotnet new is Program.cs, which is a standard C# code file:

using System;

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

This file simply defines a Main entry point (you can change the name in the project.json, if you want) that prints Hello World! to the standard output.

Running the Project

Now, let’s see how we can run the program. First, we have to restore it, which will download all the required NuGet packages:

dotnet restore

Depending on the project type, it might take a few seconds to fetch all the dependencies, since most of the .NET Core framework is published via NuGet packages. Console applications, however, don’t come with a whole lot of dependencies, so the command should execute rather quickly.

Next up, we can run the program using:

dotnet run

This will first compile (artifacts can be found in bin/Debug/netcoreapp1.0) and then run the project, printing Hello World!. Subsequent executions of dotnet run will skip the compilation unless your code files have changed.

And that’s it! You’ve successfully installed the .NET Core runtime environment and compiler, and even created and run your first project. You are now ready to learn some C# or even F# and create your first “real” project.

Leave a Reply

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