Your First C# Program
Every line of Hello World explained: using, class, Main, statements, semicolons — plus the modern top-level shortcut.
Time to dissect the classic first program, line by line:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}using System; — C#'s built-in tools are organized into named toolboxes called namespaces. Console (the screen/keyboard tool) lives in the System namespace. This line means: "I'll be using tools from System — let me call them by their short names." Without it you'd have to write System.Console.WriteLine(...) every time.
class Program — C# code lives inside classes (named containers). For now, think of the class as the box the program comes in; classes become the star of the show in Module 7.
Notice the { and } braces: every one that opens must close. The class has a pair, and inside it, Main has its own pair. Indentation doesn't change the meaning in C# — but indenting nested code is how humans (and your future teammates) read structure at a glance.
static void Main() — this is the entry point: when your program starts, execution begins at the first line inside Main and proceeds top to bottom. Every word will earn its full meaning later (static in Module 7, void and () in Module 5). For now: Main is where the program starts.
Console.WriteLine("Hello, World!"); — a statement: one complete instruction. Read it right to left: take the text "Hello, World!", hand it to the WriteLine tool of Console, which writes it to the screen and ends the line.
Two rules that generate 90% of beginner compile errors:
1. Every statement ends with a semicolon ; — it's the period at the end of the sentence.
2. C# is case-sensitive — Console works; console and CONSOLE are unknown names. WriteLine works; writeline doesn't.
When you run a C# program, where does execution begin?
- AAt the first line inside the Main method
- BAt the first line of the file, including using directives
- CAt whichever method is listed last
- DAt every method simultaneously
Now, a modern C# convenience you should recognize: top-level statements.
Since C# 9, a program file may skip the wrapper entirely and contain just the statements:
Console.WriteLine("Hello, World!");That's a complete, valid program — the compiler quietly generates the class and Main around it for you. New console projects created with dotnet new console look exactly like this. (Modern projects also auto-include common namespaces, which is why using System; can be omitted too.)
In this course, short examples are written top-level style so we can focus on the idea being taught. But you still need the full anatomy, because:
- Unity scripts are always classes — no top-level shortcut there.
- Real projects put code in many classes across many files.
- Exams love asking what
Mainis.
Both forms are the same program; one just makes you type the ceremony yourself.
This top-level program runs top to bottom. What does it print?
Console.WriteLine("Step 1");
Console.WriteLine("Step 2");
Console.WriteLine("Step 3");- AStep 1 Step 2 Step 3
- BStep 3 Step 2 Step 1
- CStep 1 Step 2 Step 3
- DError — there is no Main method
Every C# statement must end with a ___ — forgetting it is the most common beginner compile error.
Which of these lines will compile successfully?
- AConsole.WriteLine("Hi");
- Bconsole.WriteLine("Hi");
- CConsole.writeline("Hi");
- DConsole.WriteLine("Hi")