C# has always been criticized for the “Hello World” ceremony. You needed a namespace, a class, and a static Main method just to print one line. C# 9 Top-Level Programs remove this requirement, bringing C# closer to scripting languages like Python for simple tasks and microservices.
The New Entry Point
// Program.cs (The entire file)
using System;
Console.WriteLine("Hello World!");
// Defining methods at the bottom
void Log(string msg) => Console.WriteLine(msg);
Behind the Scenes
The compiler generates the `Program` class and `Main` method for you. Parameters like `args` are magically available.
// Accessing command line args
if (args.Length > 0)
{
Console.WriteLine($"Argument: {args[0]}");
}
Key Takeaways
- Ideal for **Azure Functions** and **Console Utilities**.
- Only one file in the project can have top-level statements.
- Great for learning C# without the OO overhead.
Discover more from C4: Container, Code, Cloud & Context
Subscribe to get the latest posts sent to your email.