.Net Core Hello World Console App (in Ubuntu 16.04)

Because every new framework needs another “Hello World!”

I’ve been forcing myself to try and learn this new .Net Core and in the past I’ve wrote about writing C# code on Windows and running it in Linux using the Mono framework and recently I wrote about using .Net Core with Yeoman to start an MVC application.

This however is a much simpler example to start understanding the .Net Core framework.

Here are the dependencies for this example:

  • Visual Studio Code (Download Link)
https://code.visualstudio.com
  • Install .Net Core SDK
Instructions: https://www.microsoft.com/net/core

Now let’s open a terminal and run a few commands:

mkdir hwapp
cd hwapp
dotnet new

Ok go ahead and run your new application:

dotnet restore
dotnet run

Hello World C# in Ubuntu Terminal

For those of you who read instructions you may have seen these instructions already on Microsoft’s page for installing the .Net Core Framework but what is happening when you run these commands?

hwappfolder

First .Net Core is creating some files inside of the hwapp folder when you run “dotnet new”.

  • Program.cs
  • Project.json

Lets look first at the contents of 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"
    }
  }
}

Above “version” is the version of our application which starts at 1.0.0.

Under that we have dependencies. This is where .Net Core gets interesting because it only provides the bare minimums by default (resulting in better performance) and then requires you to add dependencies for everything else you want in your application (MVC, Entity Framework, Identity, IIS Integration, etc.). These dependencies are then added to the project via Nuget when you run “dotnet restore” In this case this is only dependent on .Net Core 1.0 but it’s worth noting that even this dependency can be replaced with net46 for .Net 4.6 on Windows or even the Mono Framework.

Now lets look at Program.cs

using System;

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

If you’ve ever seen a C# Hello World Console Application this is nothing new. We can even make it slightly more interesting and add some variables:

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var hw = "Hello";
            hw += " ";
            hw += "World!";

            var question = "What is 1 + 1?";
            var math = 1+1;
            var answer = "Answer: 1 + 1 is equal to ";
            
            answer = String.Concat(answer,math);

            Console.WriteLine(hw);
            Console.WriteLine(question);
            Console.WriteLine(answer);
        }
    }
}

Here’s what it looks like in Visual Studio Code with the C# extension installed:

vscodeconsolehwapp

and here is what that code looks like when it’s running:

terminalconsolehelloworldextra

Hopefully this helps with a basic understanding of what this new cross platform framework can do and how it runs. I think it’s excellent that I was able to do this all in Linux on Ubuntu 16.04 which has now become my main OS on my home laptop and I’m excited to see more from Microsoft.

If you have any questions or comments please drop them below and feel free to share this post!

Have a question or comment? Why not leave a Reply?