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,
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
Execute dotnet build and dotnet run to execute the application. Please refer to the image below.
- dotnet build
- dotnet run
Open Program.cs in any text editor. In my case I am using gedit.
Add two using statement on the top.
- using System.Linq;
- using static System.Console;
Replace the existing code with the code shown below.
- static void Main(string[] args)
- {
- WriteLine("Enter list of number separated with space Ex: {1 2 3 4 5}:");
- var numbers = ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
- var currentValue = 0;
- var totalSwaps = 0;
-
- for (int i = 0; i < numbers.Length; i++)
- {
- int numberOfSwaps = 0;
-
- for (int j = 0; j < numbers.Length - 1; j++)
- {
- if (numbers[j] > numbers[j + 1])
- {
- currentValue = numbers[j];
- numbers[j] = numbers[j + 1];
- numbers[j + 1] = currentValue;
- numberOfSwaps++;
- }
- }
-
- totalSwaps += numberOfSwaps;
- if (numberOfSwaps == 0)
- {
- break;
- }
- }
-
- WriteLine($"Array is sorted in {totalSwaps} swaps.");
- WriteLine($"First Element: {numbers.First()}");
- WriteLine($"Last Element: {numbers.Last()}");
-
- WriteLine("\n\nPress any key ...");
- ReadKey();
- }
Execute the dotnet run to view the program execution. Please refer to the image below.
- dotnet run
Push the code into Github.
Windows 10
Get the latest from Github.
Open the Program.cs in notepad.exe. Add a comment to the Main method. Please refer to the image below.
- class Program
- {
-
-
-
- static void Main(string[] args)
- {
Execute dotnet restore and dotnet run, to view the program execution.
- dotnet restore
- dotnet run
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
Open Visual Studio Code and Open the new created GenericsDemo folder. Please refer the image.
Open Program.cs inside Visual Studio code, it will prompt for creation of required assets. Please select Yes. Please refer to the image.
It will create a folder called .vscode, within that folder it will also create two files. Please refer to the image.
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.
- {
- "version": "0.1.0",
- "command": "dotnet",
- "isShellCommand": true,
- "args": [],
- "tasks": [
- {
- "taskName": "build",
- "args": [
- "${workspaceRoot}/GenericsDemo.csproj"
- ],
- "isBuildCommand": true,
- "problemMatcher": "$msCompile"
- }
- ]
- }
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).
- {
-
-
-
- "version": "0.2.0",
- "configurations": [
- {
- "name": ".NET Core Launch (console)",
- "type": "coreclr",
- "request": "launch",
- "preLaunchTask": "build",
-
- "program": "${workspaceRoot}/bin/Debug/netcoreapp1.1/GenericsDemo.dll",
- "args": [],
- "cwd": "${workspaceRoot}",
-
- "console": "internalConsole",
- "stopAtEntry": false,
- "internalConsoleOptions": "openOnSessionStart"
- },
- {
- "name": ".NET Core Attach",
- "type": "coreclr",
- "request": "attach",
- "processId": "${command:pickProcess}"
- }
- ]
- }
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.
Switch back to Explorer View. Please refer to the image.
Create a new file within Visual Studio code and name it as ArrayPrinter.cs. Paste the below code within ArrayPrinter.cs.
- using System;
- using System.Threading;
-
- namespace GenericsDemo
- {
- public class ArrayPrinter
- {
- public ArrayPrinter PrintArray<T>(T[] arrayElements)
- {
- foreach (var current in arrayElements)
- {
- Console.WriteLine($"{current}");
- }
- Thread.Sleep (5000);
- return this;
- }
- }
- }
Replace the code inside Program.cs with the below code.
- using static System.Console;
- namespace GenericsDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var numbers = new int[] {1, 2, 3, 4, 5 };
- var vowels = new char[] {'A', 'B', 'C'};
- var names = new string[] { "Shiva", "Mathews", "Azim" };
-
- var arrayPrinter = new ArrayPrinter();
-
- arrayPrinter.PrintArray(numbers)
- .PrintArray(vowels)
- .PrintArray(names);
-
- WriteLine("\n\nPress any key ....");
- Read();
- }
- }
- }
.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 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.
We can use the integration Git within Visual Studio Code to push the code to github.
Windows 10
Get the latest version of code from GitHub.
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.
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.