.Net on Linux

Posted in linux on September 15, 2016 by Adrian Wyssmann ‐ 2 min read

.net core seems promising and can be used on linux as well. Let's have a look.

Follow [these guidelines(https://www.microsoft.com/net/core#windows) to install .Net Core for Linux. In case you are using Arch Linux, then you can get the package from the AUR (dotnet-cli 1.0.0_preview2_003131-1 as of today). After that you can start creating a project.

[adrian@archlinux]$ mkdir Hello.Net
$ cd Hello.Net
$ dotnet new

This will create a new project with two files project.json

{
  "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"
    }
  }
}

and Program.cs

using System;

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

To execute the program one has first to call dotnet restore which will restore the necessary packages specified in the project.json and the you can start the program with dotnet run

[adrian@archlinux]$ donet restore
log  : Restoring packages for /home/swtd/Projects/Hello.Net/project.json...
log  : Writing lock file to disk. Path: /home/swtd/Projects/Hello.Net/project.lock.json
log  : /home/swtd/Projects/Hello.Net/project.json
log  : Restore completed in 5268ms.

$ dotnet run
Project Hello.Net (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling Hello.Net for .NETCoreApp,Version=v1.0

Compilation succeeded.
0 Warning(s)
0 Error(s)

Time elapsed 00:00:02.7419184

Hello World!

You may now modify your Program.cs and then run again. .Net will recognize that the input was modified and compile the code again

[adrian@archlinux] $ dotnet run
Project Hello.Net (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling Hello.Net for .NETCoreApp,Version=v1.0

Compilation succeeded.
0 Warning(s)
0 Error(s)

Time elapsed 00:00:01.8648429

Hello .Net Linux

Instead of working from console and modify your files there it may be better to use an IDE. You may used download Visual Studio Code for free and install the C# extension from the marketplace or you might try MonoDevelop. I will have a look on these options and come back with some feedback in a later blog entry.