Task 3: "Hello World" in C# using the command line in Linux

techbuilder

Manmeet

Posted on September 20, 2019

Task 3: "Hello World" in C# using the command line in Linux

So the task is to write the first program to output "Hello World" on the console screen.

Honestly, I have done this so many times and things much more complex than this using C#, but I have never done it on Linux using CLI and a text editor which in this case is Sublime Text 3. There are two ways to do this and I will be discussing both in this post.

First method uses the Mono compiler which should be installed first before following the steps below:

1.open Sublime Text and create new .cs document with the following lines of code:

using System;

class HelloWorld {
static void Main() {
Console.WriteLine("Hello from Manmeet!");
}
}

2.save the file.

3.open Terminal and change the present working directory to the location where the .cs file is saved.

4.then use the command below to generate an executable file of the .cs file.

mcs -out:HelloWorld.exe HelloWorld.cs

mcs: references the 4.0-profile libraries (the APIs as defined in .NET 4.0) and supports C# 4.0

5.use the command below to run the code.

mono HelloWorld.exe

Second method uses the .Net Core CLI tools only and you don't have to install a third-party compiler like Mono for this:

1.open the command prompt and create a new folder. Navigate the folder created and then create a new console project by the following command:

$ dotnet new console

This command will create two files in your folder. First would be the .csproj project file with the dependencies necessary to build a console app and the second file is the program.cs file in which you can write the code you want and serves as the entry point for the application.

2.change the code in program.cs suitably to desired code in the text editor.

3.change directory to the project created by doing

$ cd

4.then run the code by using the command

$ dotnet run

Click on the link below for the previous post in this series:
https://dev.to/mechatrona/task-2-install-the-sublime-text-3-netcore-2-x-git-in-ubuntu-27i9

💖 💪 🙅 🚩
techbuilder
Manmeet

Posted on September 20, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related