C# 9.0 - Introduction To Top-Level Statements

Introduction

 
In this article, I am going to explain top-level statements introduced in C# 9.0. This article can be used by beginners, intermediate, and professionals.
 
Prerequisite
  • .NET 5.0
  • Visual Studio 2019 (V 16.8, Preview 3)

What is a Top-Level Statement?

 
Top-level Statement is one of the wonderful new features introduced by C# 9.0. We should have .Net 5.0 and Visual Studio 2019 (V16.8 & above) to work with it. 
 
Before we start with the discussion, let’s discuss the below code,
  1. using System;  
  2.   
  3. namespace ConsoleApplication1  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Welcome Kirtesh!!!");  
  10.         }  
  11.     }  
  12. }  
We all are aware of the above code right? It's a simple console application that will display “Welcome Kirtesh!!!”.
 
Let's see below same code in the below image,
 
 
Let’s try to list out all pieces of the above code,
  1. Using Directive eg. using System
  2. Namespaces – eg. ConsoleApplication 1. This is optional
  3. Blocks {} – Start and end block
  4. Class – Program class in above code
  5. The static main method with void returns type– Its starting point of the program.
  6. Array parameter in the main method – This is Optional.
All the above concepts we have learned in earlier versions of C# and .Net. The main method is the starting point of the program. Namespace and args[] array are optional in the above list, which means we can write code without namespace and args without any issue.
 
In C# 9.0, the Top-level statement allows you to write the same program without class and the static main method. You just write your Top-level statement and it starts working.
See the below code,
  1. using System;  

  2. Console.WriteLine("Welcome Kirtesh!!!");  

Entry point with Top-Level Statement

 
The main method is the entry point of the program, but with Top-level Statement, we don’t have the main method. That raises some questions: 
 
What is the entry point in the Top-Level Statements?
 
How will you pass args[] string array in the main method?
 
The answer is:
 
Behind the scenes, C# 9.0 will automatically generate the Main method with args[] string array.
 
What happens if multiple Top-level Statements are used in different classes?
 
We cannot have multiple main methods in earlier versions of C#. Similarly, we can have only one Top-level Statement in class. I mean we cannot have Top-level Statements in two different classes.
 
Eg. Suppose we have two classes,
  1. Program
  2. Member
If will add Top-Level Statements in both the class it will throw error “Only one compilation unit can have top-level statements”
 
How to declare Namespaces and Types with Top-level Statements
 
Top Level statements should be first before any namespaces/types in the class else it will get a compiler error message.
  1. using System;  
  2. var member = new Member(ID = 1, Name = "Kirtesh", Address = "Vadodara");  
  3.   
  4. Console.WriteLine(member.ID);  
  5.   
  6. public class Member  
  7. {  
  8.     public int ID { getset; }  
  9.     public string Name { getset; }  
  10.     public string Address { getset; }  
  11. }  
In the above code, we have Top-level Statements first and then Member class. This is the right order of declaration of namespace and Type with Top-level Statements.
 
That’s all for the Top-level statement. I hope you enjoy this article and learn new features introduced in C# 9.0.