How to Write Your First .NET Core 2.0 Application

Dhananjay Kumar / Wednesday, November 8, 2017

Microsoft .NET Core is a cross-platform open source software development framework that can be used to build applications for mobile, Windows, and the web. You can learn more about .NET Core here, but in this blog post, we’ll walk you through how to create and publish a .NET Core application for Windows.

To work with .NET Core, first you need to install it from here . While you can use any IDE to create a .NET Core application, I am going to use the Visual Studio 2017 Enterprise version. If you do not have Visual Studio installed, you may want to try the community edition, which can be found for free here.  Once the environment is set, launch Visual Studio and create a new project by selecting File->New Project-> Visual C#-> .NET Core-> Console App. Besides C#, a .NET Core application can be used in other languages like Visual Basic. 

After successfully creating your project, you will see there is already template code in the Program.cs file which looks like listing 1, shown below:

Listing 1 

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

This is the bare minimum code of a .NET Core application, which will print Hello World as output. To build a project and run the application press F5.  You will get the output as shown in the next image. 

Now, let’s modify the code to find the summation of two numbers. You can read the input from the keyword and add two numbers as shown in listing 2, below:

Listing 2

using System;
 
namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("My 1st .NET Core application");
            Console.WriteLine("Enter Number 1");
            double num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter Number 2");
            double num2 = Convert.ToDouble(Console.ReadLine());
            double result = num1 + num2;
            Console.WriteLine(num1 + " + " + num2 + " = " + result);
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);
        }
    }
}

To build this project and run the application press F5.  You will get the output as shown in the next image:

So far, we have built a basic .NET Core application. And right from Visual Studio, we can also publish this .NET Core application. To do that, right click project in the solution explorer and click on Publish

Here you can publish your app to Azure or another host. Right now, I am publishing it to a local folder, as you can see in the next image:

To publish, click on the publish button. In the Output window, you can see the publish success message as shown in the image below:

 

Now when you open the folder where you published your app, you will find various files. You need to run the .dll file to run the .NET Core application:

You can run your new .NET Core application using the command dotnetrun. Therefore, to run this application, execute command dotnetnet helloworld.dll as shown in the image below:

And as you can see, your new Windows application is published and running!