Hello World in C#
Your first C# program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first C# program.
The Code
Create a file named Program.cs:
| |
Yes, that’s it! Modern C# (9.0+) supports top-level statements, so you don’t need all the boilerplate that older tutorials show. The compiler automatically wraps this in a Main method for you.
Understanding the Code
Console.WriteLine()- Writes text to the console followed by a newline- Top-level statements - C# 9+ allows simple programs without explicit class/method declarations
Traditional Version
If you’re curious, here’s what older C# code (pre-9.0) looks like:
| |
Both versions produce identical output. The modern version is just cleaner for simple programs.
Running with Docker
The easiest way to run C# without installing the .NET SDK locally:
| |
The command creates a temporary project file, compiles your Program.cs, and runs it.
Running Locally
If you have the .NET SDK installed (version 6.0+):
| |
Expected Output
Hello, World!
Key Concepts
- C# is compiled - Source code is compiled to Intermediate Language (IL)
- IL runs on the CLR - Common Language Runtime executes the bytecode
- Top-level statements - Modern C# allows simple programs without ceremony
Consoleis inSystem- TheSystemnamespace is implicitly imported in modern C#
.NET SDK vs Runtime
- SDK: For development - includes compiler, libraries, and CLI tools
- Runtime: For running apps only - smaller, production-focused
We use the SDK image for development because we need to compile the code.
Next Steps
Continue to Variables and Data Types to learn about storing and manipulating data in C#.
Running Today
All examples can be run using Docker:
docker pull mcr.microsoft.com/dotnet/sdk:9.0