CSharp 9.0 - Introduction To Top-Level Statements

In C# 9.0, top-level statements are syntax features that allow developers to write code directly at the top of the file. Without the need for a class and methods. This feature was introduced in November 2020 as part of .NET 5.

Before C# 9.0, every C# program required an entry point, the Main method, defined within a class like this:

csharp
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }

Although the above code is obvious, it includes boilerplate rules. Using top-level statements, you can skip the Main path and write your program logic directly at the top of the file.

csharp
// Top-level statements in C# 9.0 using System; Console.WriteLine("Hello, World!");

List of features to know about the top-level statements.

Simplifies code

You can simplify the code by using the top-level statements. It can reduce code by removing the Main method and its associated class.

Good for small applications

Top-level statements are important for simplifying smaller applications, such as Azure Functions and GitHub Actions.

Not recommended for large apps

Top-level statements are not well structured so they might not be suitable for large or complex apps that need more structure.

Only use in one file

Top-level statements can only be used in one file per project, usually Program.cs.

The compiler generates a Program class

The compiler generates a Program class with an entry point method for the application, but the method name is not Main.

Not mandatory

Top-level statements are optional so that, developers can choose whether or not to use them.

How Do Top-Level Statements Work?

With top-level statements, the compiler generates the main method for you. You simply write your code as if you were inside the body of the main method, and the compiler takes care of the rest.

Example

Let’s create the example by reading command-line arguments using top-level statements:

Without top-level statement

csharp
using System; class Program { static void Main(string[] args) { if (args.Length > 0) { Console.WriteLine($"Hello, {args[0]}!"); } else { Console.WriteLine("Hello, World!"); } } }

Output

Hello, World!

With top-level statement

csharp
using System; if (args.Length > 0) { Console.WriteLine($"Hello, {args[0]}!"); } else { Console.WriteLine("Hello, World!"); }

Output

Hello, World!

Here, args are still available, even though we have not explicitly defined the Main method. The compiler automatically sends the command line to top-level statements.

Example

Top-level statements also work with asynchronous code. For asynchronous tasks, simply mark the top-level entry as async:

Without top-level statement

csharp
using System; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { await PrintMessageAsync(); } static async Task PrintMessageAsync() { await Task.Delay(1000); Console.WriteLine("Async operation completed."); } }

Output

Async operation completed.


With top-level statement

csharp
using System; using System.Threading.Tasks; await Task.Delay(1000); Console.WriteLine("Async operation completed.");

Output

Async operation completed.

Again, we notice how concise the code becomes. The async keyword applies to the automatically generated main method.

line

Copyrights © 2024 letsupdateskills All rights reserved