C#C# · Lesson 1 of 9

Hello, World!

C# has evolved dramatically. Modern C# (12+) lets you write Hello World in one line. Old C# needed a class, a namespace, and a static void Main. We'll show both, but live in the modern world.

Modern C# (9+) supports "top-level statements" — you can write code directly without wrapping it in a class. The compiler generates the boilerplate for you. For reference, we'll also show the traditional form.

C#
// Modern C# (top-level statements) — Program.cs
Console.WriteLine("Hello, World!");
C#
// Traditional form (still valid, required in some contexts)
using System;

namespace HelloApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.WriteLine("My name is {0} and I am {1}.", "Alice", 30);

            // String interpolation (modern, preferred)
            string name = "World";
            Console.WriteLine($"Hello, {name}!");
            Console.WriteLine($"2 + 2 = {2 + 2}");

            // Console.Write — no newline
            Console.Write("Loading");
            Console.Write("...");
            Console.WriteLine("done!");
        }
    }
}
Bash
dotnet new console -n HelloWorld
cd HelloWorld
# Edit Program.cs, then:
dotnet run
◆ Note
C# files have the .cs extension. A C# project is defined by a .csproj file. dotnet run handles compilation and execution. For production builds, use dotnet build then dotnet <project>.dll.