![](/img/trans.png)
[英]How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements?
[英]What are top level statements in C#? What is their use?
我剛剛創建了我的第一個 .NET 6 控制台應用程序,而不是默認的,
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
我有:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
即使沒有 class,程序也會運行,我查看了評論中提供的鏈接。 但我不知道這是新功能還是舊功能,如果這是新功能? 這是否意味着 C# 以后將允許 C 風格的編程?
這是 C# 9 或 10 的新功能。Microsoft 文檔說明如下:
通過將程序的入口點放在 class 中的 static 方法中,頂級語句使您能夠避免所需的額外儀式。 新控制台應用程序的典型起點類似於以下代碼:
using System;
namespace Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
上述代碼是運行 dotnet new console 命令並創建新控制台應用程序的結果。 這 11 行只包含一行可執行代碼。 您可以使用新的頂級語句功能簡化該程序。 這使您可以刪除該程序中除兩行之外的所有行:
Console.WriteLine("Hello, World!");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.