Top Level Statement in C# 9.0

Top Level Statement in C# 9.0

C# 9.0 introduced a new feature called Top Level Statement. Top-Level Statement allows developers to write programs without explicitly defining the class or main method. Until C# 9.0, It was all about the Main() method where program control start and ends. With C# 9.0, you don’t need to mention the Main() method or Class definition explicitly using Top-Level Statement. Let us take a look at how to use the Top-Level statement in C# and how it works internally.

Top-Level Statement in C# 9.0

A typical Hello World Console Application for C# looks as below

using System;

namespace TopLevelStatementCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

With the above code executed, you will find typical “Hello World!” output in the console! and nothing new here.

Basic Hello World C# Code
Default C# “Hello World” Code for Console Application

With Top-Level Statement in C# 9.0, you can achieve the same using just following line of code.

System.Console.WriteLine("Hello World!");

Which will produce the same output as previous code block.

Like the basic statement of execution, you can pass the command line argument for the top-level statement. You can pass as many as an argument you want.

System.Console.WriteLine($"Hello {args?[0]}");

Following image shows an quick example of passing command line argument for Top-Level Statement

command line argument with top-level statement in C#

You can provide the Command line argument for any console application from Project Properties > Debug > Application Arguments

Popular Tips from Daily .NET Tips : How to Pass Command Line Arguments using Visual Studio ?

If you have any complication error for this, you have to ensure you have the selected “Targeted Framework” to .NET 5.0. This setting available under Project Properties -> Application

"Targeted Framework" to .NET 5.0

This is simple and reduce developer time using typing repetitive statement.

Behind the Scene

While as a developer we don’t write all these additional codes, behind the scenes C# Complier does the job for us. We can easily explore this using the ILDASM Tool and inspecting the DLLs generated by the C# Code

The following image shows the IL Code generated for the started C# Main Method Code, which clearly defined the Main() method, which takes an argument and is the executions’ entry point. Idstr defined the constant string for “Hello World!” which is getting printed in the following statement.

IL Code for C#

Similarly for Top-Level Statement as well, the IL Generated code show the same set of IL Block that would be executed.

IL Code for command line statement

This clearly shows the complier generates the similar code in the behind and get that executed which abstracted some line of code execution.

Planning to start learning Azure – Check this out : 10 Azure Cloud services that every Developers, Consultant, and Architects should Know and Learn it well

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.