How to build and run c sharp project from the terminal

Published by admin on

Build and run C# program from the terminal

Running a C# program from the terminal :

In this tutorial, I will show you how to create one simple ‘hello world’ application in C# and how to run that using the terminal.

I am using Mac while writing this article, but the process is the same for Windows and Linux as well.

 

Steps :

  • You will have to download dot net core sdk from this link first.
  • Download the installer file and install it on your system.
  • Now, open one terminal, type dotnet and hit enter. You will see help messages like below on the terminal :
Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET Core information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.

run c sharp program from the terminal

  • Move to a folder on the terminal you want to create your project.
  • Type dotnet new console and hit enter. It will create one console application with one Program.cs file along with one C# project file HelloWorld.csproj. The .cs file has one ‘Hello World’ program already written.
using System;

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

c sharp program on terminal

  • This step is optional. If you are using one .NET Core 1.x version, type dotnet restore. It will provide you access to all .NET core packages that are required to build the project. If you have .NET Core 2.x version, this step is not required. You can check the version by simply typing dotnet –version on a terminal.
  • For running the project, type dotnet run and hit enter.

c sharp on terminal

Depending on the OS and Dotnet version you are using, the steps we have shown above may not work. I would recommend you to check the Microsoft docs to know more about running a .net project.

Categories: c#

Leave a Reply

Your email address will not be published. Required fields are marked *