Hello World in C#

Our first program will print the classic “hello world” message. Here’s the full source code.

using System;

class Program {
    static void Main() {
        Console.WriteLine("hello world");
    }
}

To run the program, save the code in a file named Program.cs and then use the csc compiler to compile it, followed by running the resulting executable.

$ csc Program.cs
$ Program.exe
hello world

In environments where you need to build your program into an executable binary, the C# compiler (csc) by default produces an executable file that you can run directly.

$ csc Program.cs
$ ls
Program.exe    Program.cs

We can then execute the compiled binary directly.

$ Program.exe
hello world

Now that we can run and build basic C# programs, let’s learn more about the language.