Top-level statements
The .NET team introduced top-level statements to the language in .NET 5 and C# 9. From that point, writing statements before declaring namespaces and other members became possible. Under the hood, the compiler emits those statements in a Program.Main
method.
With top-level statements, a minimal .NET “Hello World” console program looked like this (Program.cs
):
using System;
Console.WriteLine("Hello world!");
Unfortunately, we still need a project to run it, so we have to create a .csproj
file with the following content (for example, MyProject.csproj
):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>
From there, we can use the .NET CLI to dotnet run
the application, and it will output the following in the console before the program terminates...