Exploring top-level programs, functions, and namespaces
Since .NET 6, the default project template for console apps uses the top-level program feature introduced with .NET 5. It is important to understand how it works with the automatically generated Program
class and its <Main>$
method.
Let’s explore how the top-level program feature works when you define functions:
- Use your preferred coding tool to create a new project, as defined in the following list:
- Project template: Console App /
console
- Project file and folder:
TopLevelFunctions
- Solution file and folder:
Chapter01
- Do not use top-level statements: Cleared
- Enable native AOT publishing: Cleared
- Project template: Console App /
- In
Program.cs
, delete the existing statements, define a local function at the bottom of the file, and call it, as shown in the following code:using static System.Console; WriteLine("Top-level functions example"); WhatsMyNamespace...