[转]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,
复制代码
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,
 

 

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,
 
using System;  
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.
 
复制代码
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.
posted @   路边两盏灯  阅读(124)  评论(0编辑  收藏  举报
编辑推荐:
· 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列表的办法
点击右上角即可分享
微信分享提示