Understanding Top Level Programs
Explore how top level programs in C# 10 and .NET 6 improve development by automatically generating boilerplate code for console apps. Understand the structure and restrictions of these programs, including implicit System namespace imports, and learn to reveal the hidden compiler-generated code for deeper insight.
Top level programs
If we have seen older .NET projects before, we might have expected more code, even just to output a simple message. This is because some code is written for us by the compiler when we target .NET 6 or later.
If we had created the project with .NET SDK 5.0 or earlier or selected the check box with “Do not use top-level statements” label, then the Program.cs file would have more statements, as shown in the following code:
During compilation with .NET SDK 6.0 or later, all the boilerplate code to define a namespace, the Program class, and its Main method is generated and wrapped around the ...