Modern C# has declared war on "Boilerplate." In the past, writing a simple 5-line program required 20 lines of ceremonial code (namespaces, classes, and main methods). Modern C# allows you to write executable code directly at the root of a file, mimicking the simplicity of Python or Node.js.
Before C# 9, even a "Hello World" needed a complex structure that confused beginners and annoyed professionals.
using System;
namespace MyProject
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
Now, everything in Program.cs is implicitly wrapped in a hidden Main method by the compiler. You can just start writing!
// Just one line. That's it.
Console.WriteLine("Hello World");
Do you get tired of typing using System.Collections.Generic; at the top of every single file? Modern C# allows you to define your imports globally in one place.
// In a file like GlobalUsings.cs
global using System.Text.Json;
global using MyProject.Core.Interfaces;
// These namespaces are now automatically available in EVERY .cs file
// in your project without needing a single 'using' statement!
Q: "Can I have multiple files with Top-Level Statements in the same project?"
Architect Answer: "No. You can only have exactly ONE file with top-level statements per compilation unit (project). Because top-level statements are implicitly translated into the static 'Main' method of the assembly by the compiler, having two files with top-level statements would result in a 'Program has more than one entry point defined' compiler error. Usually, we reserve this simplicity for the `Program.cs` file of our entry project."