New in .NET 10.0 [15]: Program class and Main() method in file-based apps

File-based apps can optionally have Top-Level Statements or a classic .NET entry point.

listen Print view
Traffic sign with inscription .NET

(Image: Pincasso / Shutterstock.com)

1 min. read
By
  • Dr. Holger Schwichtenberg

A file-based app can use the Top-Level Statements introduced in C# 9.0 (as part of .NET 5.0). This will be the standard case where execution of the file begins with the first line:

Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
Console.WriteLine($"Kompilierungsmodus: {(System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported ? "JIT" : "AOT")}");

Starting the file-based app with Top-Level Statement (Fig. 1)

Videos by heise

In addition to using Top-Level Statements, the classic style with class Program and Main() method is also possible in file-based apps:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
        Console.WriteLine($"Kompilierungsmodus: {(System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported ? "JIT" : "AOT")}");
    }
}

The file-based app can also be started with the Main() method in the Program class (Fig. 2).

(olb)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.