How do I compile and running a C# program?

The C# compiler compiles our source code into an executable format that can be run directly from the command prompt or double clicking the executable file. A C# program source code usually has a .cs as the file extension, but we can actually use any extension.

Now, let's say that we have a HelloWorld.cs, to compile the code we use the csc command, which stands for C# compiler. The compiler file name is csc.exe, this compiler is part of the Microsoft .NET SDK.

Here is our HelloWorld snippet:

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

To compile it type the following command.

D:\Temp>csc HelloWorld.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

The output printed by the compiler is depends on the compiler that you have in your system. This process will produce a file called HelloWorld.exe.

Running the program produces the following output:

D:\Temp>HelloWorld.exe
Hello World!