.Net Core Console Applications On Ubuntu 14.04 And Windows 10

Introduction

This article demonstrates how to create a console application on Ubuntu 14.04 and Windows 10 using .Net Core. This article gives the details of OS versions, Tools set and 4 demos.

The code tested on below Operating System Versions,

  • Ubuntu 14.04
  • Windows 10

Tools Set,

  • .Net Core 1.0.4
  • .Net Command Line Interface (cli) Tools
  • Text Editors: gedit (Ubuntu 14.04), and notepad (Windows)
  • Visual Studio Code (1.12.2)
  • Git
  • GitHub URL

Demo 1 - Console application using terminal, cli, and text editor

Ubuntu 14.04

Create a folder called “SortDemo”. Navigate to the Folder. Execute dotnet new console command to create a new project. Execute dotnet restore to restore the dependencies of the project. Please refer to the image below.

  • dotnet new
  • dotnet restore

    .Net Core

Execute dotnet build and dotnet run to execute the application. Please refer to the image below.

  • dotnet build
  • dotnet run

    .Net Core

Open Program.cs in any text editor. In my case I am using gedit.

.Net Core

Add two using statement on the top.

    1. using System.Linq;
    2. using static System.Console;
 
Replace the existing code with the code shown below.

  1. static void Main(string[] args)  
  2. {  
  3.     WriteLine("Enter list of number separated with space Ex: {1 2 3 4 5}:");  
  4.     var numbers = ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();  
  5.     var currentValue = 0;  
  6.     var totalSwaps = 0;  
  7.   
  8.     for (int i = 0; i < numbers.Length; i++)  
  9.     {  
  10.         int numberOfSwaps = 0;  
  11.   
  12.         for (int j = 0; j < numbers.Length - 1; j++)  
  13.         {  
  14.             if (numbers[j] > numbers[j + 1])  
  15.             {  
  16.                 currentValue = numbers[j];  
  17.                 numbers[j] = numbers[j + 1];  
  18.                 numbers[j + 1] = currentValue;  
  19.                 numberOfSwaps++;  
  20.             }  
  21.         }  
  22.   
  23.         totalSwaps += numberOfSwaps;  
  24.         if (numberOfSwaps == 0)  
  25.         {  
  26.             break;  
  27.         }  
  28.     }  
  29.   
  30.     WriteLine($"Array is sorted in {totalSwaps} swaps.");  
  31.     WriteLine($"First Element: {numbers.First()}");  
  32.     WriteLine($"Last Element: {numbers.Last()}");  
  33.   
  34.     WriteLine("\n\nPress any key ...");  
  35.     ReadKey();  
  36. }   

Execute the dotnet run to view the program execution. Please refer to the image below.

  • dotnet run

    .Net Core

Push the code into Github.

Windows 10

Get the latest from Github.

.Net Core

Open the Program.cs in notepad.exe. Add a comment to the Main method. Please refer  to the image below.

  1. class Program  
  2. {  
  3.     /// <summary>  
  4.     /// Same code works in Ubuntu 14.04 and Windows 10.   
  5.     /// </summary>  
  6.     static void Main(string[] args)  
  7.     {  

Execute dotnet restore and dotnet run, to view the program execution.

  • dotnet restore
  • dotnet run

    .Net Core

Demo 2 - Console application using terminal, cli, and Visual Studio Code

Ubuntu 14.04

Create a folder called “GenericsDemo”. Navigate to the Folder. Execute dotnet new console command to create a new project. Execute dotnet restore to restore the dependencies of the project. Please refer  to the image below.

  • dotnet new
  • dotnet restore

    .Net Core

Open Visual Studio Code and Open the new created GenericsDemo folder. Please refer the image.

.Net Core

Open Program.cs inside Visual Studio code, it will prompt for creation of required assets. Please select Yes. Please refer to the image.

.Net Core

It will create a folder called .vscode, within that folder it will also create two files. Please refer to the image.

.Net Core

Tasks.json

Is used to run npm, MSBuild, maven and other command line tools. In our case the args argument specifies which project to build. Please refer to the image below.

  1. {  
  2.     "version""0.1.0",  
  3.     "command""dotnet",  
  4.     "isShellCommand"true,  
  5.     "args": [],  
  6.     "tasks": [  
  7.         {  
  8.             "taskName""build",  
  9.             "args": [  
  10.                 "${workspaceRoot}/GenericsDemo.csproj"  
  11.             ],  
  12.             "isBuildCommand"true,  
  13.             "problemMatcher""$msCompile"  
  14.         }  
  15.     ]  
  16. }  

Launch.json

Specifies the list of attributes which will be used while executing/debugging the application. In our case type (coreclr), request (launch / attach), preLaunchTask (build), program (path to binary which will be launched).

  1. {  
  2.    // Use IntelliSense to find out which attributes exist for C# debugging  
  3.    // Use hover for the description of the existing attributes  
  4.    // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md  
  5.    "version""0.2.0",  
  6.    "configurations": [  
  7.         {  
  8.             "name"".NET Core Launch (console)",  
  9.             "type""coreclr",  
  10.             "request""launch",  
  11.             "preLaunchTask""build",  
  12.             // If you have changed target frameworks, make sure to update the program path.  
  13.             "program""${workspaceRoot}/bin/Debug/netcoreapp1.1/GenericsDemo.dll",  
  14.             "args": [],  
  15.             "cwd""${workspaceRoot}",  
  16.             // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window  
  17.             "console""internalConsole",  
  18.             "stopAtEntry"false,  
  19.             "internalConsoleOptions""openOnSessionStart"  
  20.         },  
  21.         {  
  22.             "name"".NET Core Attach",  
  23.             "type""coreclr",  
  24.             "request""attach",  
  25.             "processId""${command:pickProcess}"  
  26.         }  
  27.     ]  
  28. }  

Click on the Debug icon on the left bar. It will display Debug blade. Under the Debug dropdown it will show two options/configurations which were defined in launch.json. .Net Core Launch (console) option will help us launch/execute/debug the code within Visual Studio Code. .Net Core Attach will help us debug the code, when the execution is done in separate terminal and debug the code within Visual Studio code. Please refer to the image.

.Net Core

Switch back to Explorer View. Please refer to the image.

.Net Core

Create a new file within Visual Studio code and name it as ArrayPrinter.cs. Paste the below code within ArrayPrinter.cs.

  1. using System;  
  2. using System.Threading;  
  3.   
  4. namespace GenericsDemo  
  5. {  
  6.     public class ArrayPrinter  
  7.     {  
  8.         public ArrayPrinter PrintArray<T>(T[] arrayElements)  
  9.         {  
  10.             foreach (var current in arrayElements)  
  11.             {  
  12.                 Console.WriteLine($"{current}");  
  13.             }  
  14.             Thread.Sleep (5000);  
  15.             return this;  
  16.         }  
  17.     }  
  18. }  

Replace the code inside Program.cs with the below code.

  1. using static System.Console;  
  2. namespace GenericsDemo  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             var numbers = new int[] {1, 2, 3, 4, 5 };  
  9.             var vowels = new char[] {'A''B''C'};  
  10.             var names = new string[] { "Shiva""Mathews""Azim" };  
  11.   
  12.             var arrayPrinter = new ArrayPrinter();  
  13.   
  14.             arrayPrinter.PrintArray(numbers)  
  15.                         .PrintArray(vowels)  
  16.                         .PrintArray(names);  
  17.   
  18.             WriteLine("\n\nPress any key ....");  
  19.             Read();  
  20.         }  
  21.     }  
  22. }  

.Net Core Launch

Enable a break-point on line number 10 inside ArrayPrinter.cs. Click on Debug Icon, and either click on “Start Debuggin” Green triangle OR press F5 from key board. You will see the execution and control stops at the break point. We can use the locals, watch, and call stack windows to debug the code.

.Net Core

.Net Core Attach

Switch to Debug mode, select .Net Core Attach from the dropdown. Open a Terminal window, and execute the dotnet run command. As we have already have Thread.Sleep(5000), program will execute little slower. Immediately switch back to Visual Studio Code and click “Start Debugging” icon. We will see the program will hit the break point.

.Net Core

We can use the integration Git within Visual Studio Code to push the code to github.

.Net Core

Windows 10

Get the latest version of code from GitHub.

.Net Core

Open the GenericsDemo folder inside Visual Studio Code. Execute dotnet restore command from within the integrated terminal. Please refer to the image. Switch to Debug mode and press F5 to run the code in Windows 10.

.Net Core

Summary

In this article, I discussed how we can create console applications using .net code and C#. We also saw how to create these in both Ubuntu 14.04 and Windows 10. We also saw the same code works on both Ubuntu 14.04 and Windows 10.