Getting Started With .NET Core 2.0

Microsoft just announced the next version of its open source software development framework, .NET Core. The new version, i.e., .NET Core 2.0 is much improved and mature version compare to its predecessor, 1.0.

In this tutorial, we will see how to get started with .NET Core 2.0 and how to build our first .NET Core 2.0 applications.

What .NET Core Is

Microsoft’s .NET Core is a cross-platform, unified, fast, lightweight, modern, open-source framework for building mobile, Web, and Windows applications and services that can be deployed and run on Windows, Linux and Mac operating systems.

.NET Core is Cross-platform

.NET Core is a cross-platform framework that allows developers to develop, deploy, and run applications and services on operating systems including Windows, Linux and macOS.

.NET Core is Unified

.NET Core offers a single unified component (.NET Standard library) to target all platforms with the same code, the same languages, and the same tools.

.NET Core is Fast

.NET Core is blazing fast. The .NET Core based applications not only give better response times but also require less compute power. The current release of .NET Core is a high-performance server runtime for Windows Server and Linux, making .NET 8x faster than Node.js and 3x faster than Go.

.NET Core is Lightweight

.NET Core is a lightweight framework. The .NET Core allows developers to deploy libraries and components that are needed in a set of packages.

.NET Core is Modern

.NET Core is developed keeping in mind today’s and tomorrow’s development needs. The .NET Core supports modern programming language features like generics, LINQ, async support, and tuples via its supporting languages C#, VB, and F#.

.NET Core is Open Source

.NET Core is open source. All .NET Core runtime, libraries, compilers, languages, and tools are all open source and available on GitHub to download and contribute.

Getting Ready

Before you start writing code, you must install .NET Core and related tools on your machine. You can download the latest version of .NET Core here,

There are three ways for you to start building .NET Core applications – command line, Visual Studio Code, and Visual Studio. We will use Visual Studio 2017 Community version 15.3 or later versions.

Visual Studio 2017 has three different editions - Enterprise, Professional, or Community. If you don’t have a Visual Studio license, I recommend using the free version, Visual Studio 2017 Community edition. You can download it here.

After the download is completed, run the .exe to install it.

During the installation process, make sure, the “.NET Core cross-platform development” workload is selected. This will ensure installing .NET Core and tools to support .NET Core development in Visual Studio IDE. See Figure 1.

.NET Core 2.0
Figure 1.

Hello, .NET Core!

Now, you’ve installed Visual Studio 2017, let’s build your first Hello World! .NET Core application.

Open Visual Studio 2017 and create a new project by selecting New > Project.

You will see .NET Core on the left side of templates while on the right side, you will see various .NET Core templates as listed in Figure 2.

.NET Core 2.0
Figure 2.

Select the Console App (.NET Core), give your project a name, select a folder where you want your project to be created, and click OK. This will create your first .NET Core project.

I give my project the name “Hello World”.

The Visual Studio project looks like Figure 3.

.NET Core 2.0
Figure 3.

The default program has a Program.cs file that has the main code listed. This is where you will be writing more code. See Listing 1. I add an extra line, Console.ReadKey(); to the code. By adding this line, the console window waits until a key is pressed.

  1. using System;  
  2.   
  3. class Program  
  4. {  
  5.     static void Main(string[] args)  
  6.     {  
  7.         Console.WriteLine("Hello World!");  
  8.         Console.ReadKey();  
  9.     }  
  10. }  

Listing 1.

Build and run your project by hitting F5 or Fn+F5, or by selecting Debug > Start Debugging from the drop-down menu.

The output looks like Figure 4.

.NET Core 2.0
Figure 4.

Congratulations! You just built and ran your very first .NET Core app.

What just happened here?

You have now written your first few lines of C# code. But what does each line of your program mean? Let me describe the various components of your “Hello, C# World!” program.

The first line of your program is this:

  1. using System;  

The .NET framework class library is referenced in namespaces. The System namespace contains the Console class, which reads from or writes to the console.

The class keyword defines a new class that is followed by a class name, as seen in the second line of the “Hello, C# World!” code listing 2.

  1. class Hello  
  2. {  
  3.     …..  
  4. }  

Listing 2.

The next line of code is the static void Main() method. See Listing 3.

  1. static void Main()  
  2. {  
  3.     Console.WriteLine("Hello, C# World!");  
  4.     Console.ReadKey();  
  5. }  

Listing 3.

In C#, every program must have a static Main() or int Main() entry point. The concept is similar to that of the Main() function of C++. This means, this is what a compiler will be looking for to start the program and whatever code is written in this method will be executed before anything else.

The Console class is defined in the System namespace. You can access its class members by referencing them directly. The Console.Writeline() method writes a string and a line terminator to the console and the Console.ReadKey() method reads a keystroke.

Each code block in C# must be inside enclosed parentheses {..} and each line must end with a character ‘;’. In the code Listing 1, class Hello starts with a ‘{‘ and ends with a ‘}. Similarly, the code block for method Main starts with a ‘{‘ and ends with a ‘}’. By misplacing these parentheses will result in a compiler error. 

Extending Hello .NET Core!

Now, let’s add more functionality to our Hello .NET Core! app.

In the extended version of the app, we will read two numbers and an operation type (add, subtract, multiply, or divide) from the console and the program will perform the given operation on two numbers and display the result.

The code in Listing 4 reads the numbers and use a switch..case statement to find the operation type and display the results after the operation is performed.

  1. using System;  
  2. namespace HelloWorld  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Console.WriteLine("Hello World!");  
  9.           
  10.             // Read first number   
  11.             Console.Write("Enter Number 1: ");  
  12.             var var1 = Console.ReadLine();  
  13.             // Convert string to int  
  14.             int num1 = Convert.ToInt32(var1);  
  15.   
  16.             // Read second number  
  17.             Console.Write("Enter Number 2: ");  
  18.             var var2 = Console.ReadLine();  
  19.             // Convert string to int  
  20.             int num2 = Convert.ToInt32(var2);  
  21.   
  22.             // Read operation type - operator  
  23.             Console.Write("Enter one Operator (Add/Sub/Mul/Div): ");              
  24.             var op = Console.ReadLine();                    
  25.               
  26.             switch (op)  
  27.             {  
  28.                 case "Add":  
  29.                     Console.WriteLine("Add {0}:", num1 + num2);  
  30.                     break;  
  31.                 case "Sub":  
  32.                     Console.WriteLine("Sub {0}:", num1 - num2);  
  33.                     break;  
  34.                 case "Mul":  
  35.                     double result = num1 * num2;  
  36.                     Console.WriteLine("Div {0}:", result);  
  37.                     break;  
  38.                 case "Div":  
  39.                     double res = (double)num1 / num2;  
  40.                     Console.WriteLine("Div {0}:", res);  
  41.                     break;  
  42.                 default:  
  43.                     Console.WriteLine("Operation failed!");  
  44.                     break;  
  45.             }  
  46.               
  47.             Console.WriteLine("Press any key to exit...");  
  48.             Console.ReadKey(true);              
  49.         }  
  50.     }  
  51. }  

Listing 4.

The output will look like Figure 5.

.NET Core 2.0
Figure 5.

Publish A .NET Core App

You can use Visual Studio to build, debug, and publish your app.

We will publish the release version of an assembly. Change the build option in your Visual Studio from Debug to Release. See Figure 6.

.NET Core 2.0
Figure 6.

Now, right click on your project name and select Publish menu option. The Publish option builds and creates the necessary files to run your app. See Figure 7.

.NET Core 2.0
Figure 7.

On the following screen in Figure 8, you need to provide a destination folder, where all files will be copied. I kept the default folder but you may use the Browse button to change the folder location.  Click the Publish button to start publishing your app.

.NET Core 2.0
Figure 8.

The Output window shows the progress of the publishing process. Once successfully published, you will see Build succeeded. See Figure 9.

.NET Core 2.0
Figure 9.

Now, if you open the folder where you published your app files, you will see a .dll, a .pdb, and two .json files. The .pdb file is for debugging and not required for the publishing an app. See Figure 10.

.NET Core 2.0
Figure 10.

Run .NET Core App

To run your .NET app, all you need to do is to go to the location of your .dll and use the following command.

dotnet helloworld.dll

The output looks like Figure 11, where your app is ready to go. Simply provide all input fields and see the results.

.NET Core 2.0
Figure 11.

Summary

.NET 2.0 is an open source, cross-platform, modern, light, and fast framework for the web and mobile app development. In this tutorial, we saw how to get ready, build, deploy, and run our first .NET Core 2.0 console app.

Here are some useful links on .NET Core.

Up Next
    Ebook Download
    View all
    Learn
    View all