我刚刚创建了我的第一个 .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!");
即使没有上课,程序也会运行!我看了一下评论中提供的链接,但我不知道这是新功能还是旧功能。如果这是一项新功能,是否意味着 C# 以后将允许使用 C 风格的编程?
这是 C# 9 或 10 的新功能。Microsoft 文档 说如下:
顶级语句使您能够避免将程序的入口点放在类的静态方法中所需的额外仪式。新控制台应用程序的典型起点类似于以下代码:
using System;
namespace Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
以上代码是运行dotnet new console命令,新建控制台应用的结果。这 11 行仅包含一行可执行代码。您可以使用新的顶级语句功能简化该程序。这使您能够删除该程序中除两行之外的所有行:
Console.WriteLine("Hello, World!");