How to Execute C# Program on cmd (command-line)?

Last Updated : 28 Apr, 2026

C# is a modern, object-oriented programming language developed for the .NET platform. It is widely used for building desktop, web, and enterprise applications.

  • Part of the Common Language Infrastructure (CLI)
  • Syntax similar to C++ and Java, making it easy to learn

Sample C# Program to execute on Command-Line:

csharp
using System;

// namespace declaration
namespace HelloWorldApp {

// Class declaration
class Geeks {

    // Main Method
    static void Main(string[] args)
    {

        // statement
        // printing Hello World!
        Console.WriteLine("Hello World!");

        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
}
}

Setting up the Environment for C# Compiler

Step 1: Go to Control Panel -> System and Security -> System. Under Advanced System Settingoption click on Environment Variablesas shown below:

Advanced System SettingsSetting up Environment variable

Step 2: Now, we have to alter the "Path" variable under System variables so that it also contains the path to the .NET Framework environment. Select the "Path" variable and click on the Edit button as shown below:

Environment-variable-setup-02-1

Step 3: We will see a list of different paths, click on the New button and then add the path where .NET Framework is installed.

CSharp-Command-Prompt-Environment-Setup

Step 4: Click on OK, Save the settings and it is done !! Now to check whether the environment setup is done correctly, open command prompt and type csc.

CSharp-Command-Prompt-02

Steps to Execute C# Program on cmd

Step 1: Open the text editor like Notepad or Notepad++, and write the code that you want to execute. Now save th

CSharp-Command-Prompt-00

Step 2: Compile your C# source code with the use of command:

csc File_name.cs

If your program has no error then it will create a filename.exe file in the same directory where you have saved your program. Suppose you saved the above program as Hello.cs. So you will write csc Hello.cs on cmd. This will create a Hello.exe file.

CSharp-Command-Prompt-03

Step 3: Now there are two ways to execute the Hello.exe.

  • First, you have to simply type the filename i.e Hello on the cmd and it will give the output.
  • Second, you can go to the directory where you saved your program and there you find filename.exe. You have to simply double-click that file and it will give the output.
  • Using Command:CSharp-Command-Prompt-04
  • Using .exe file:CSharp-Command-Prompt-05
Comment
Article Tags:

Explore