[转]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.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-05-18 【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法