C#C#
BeginnerLesson 1 of 16

Hello World & Top-Level Statements – C#

Your first C# program and how output works.

Since C# 9 you no longer need a class or a Main method: the code in Program.cs can start right away (top-level statements). You print a line with Console.WriteLine.

Console.WriteLine("Hello World!");
Console.WriteLine("I am learning " + "C#");
Console.WriteLine(42);

Write prints without a line break, WriteLine with one. Comments start with // (single line) or are enclosed in /* ... */. Special characters such as \n (new line) and \t (tab) work in strings.

// This is a comment
Console.Write("No line break");
Console.WriteLine(" – continues on the same line");
Console.WriteLine("a\tb\tc");
Console.WriteLine("Line 1\nLine 2");

The classic form with Main still exists – you will see it in older tutorials and books. Both lead to the same program:

namespace MeinProjekt;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello from Main");
    }
}
TipTop-level statements may appear in only one file per project. Classes and records that you define yourself go below the statements (or into separate files).