[转]C# 9.0 - Introduction To Top-Level Statements
原文链接: https://www.c-sharpcorner.com/article/c-sharp-9-0-top-level-statement/#:~:text=Top%20Level%20statements%20should%20be%20first%20before%20any,Top-level%20Statements.%20That%E2%80%99s%20all%20for%20the%20Top-level%20statement.
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,
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,
- Using Directive eg. using System
- Namespaces – eg. ConsoleApplication 1. This is optional
- Blocks {} – Start and end block
- Class – Program class in above code
- The static main method with void returns type– Its starting point of the program.
- 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,
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,
- Program
- 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.
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.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!