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

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome Kirtesh!!!");
        }
    }
}

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

C# image

Let’s try to list out all the 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 the 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

using System;  

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

Entry point with Top-Level Statement

The main method is the entry point of the program, but with the 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 classes it will throw the 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.

using System;  
var member = new Member(ID = 1, Name = "Kirtesh", Address = "Vadodara");  
  
Console.WriteLine(member.ID);  
  
public class Member  
{  
    public int ID { get; set; }  
    public string Name { get; set; }  
    public string Address { get; set; }  
}  

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.


Similar Articles