C# 9.0 支持 Visual Studio 2019 中的顶级程序 [英] C# 9.0 Support for Top-level programs in Visual Studio 2019

查看:23
本文介绍了C# 9.0 支持 Visual Studio 2019 中的顶级程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 C# 9.0 和 .NET 5 的到来,引入了一项称为顶级程序"的新功能.

With the coming of C# 9.0 and .NET 5 a new feature is getting introduced called "Top-level programs".

此功能无需将代码包装在通常的命名空间/类/Main 方法中,从而消除了创建简单 C# 应用程序所需的大量样板代码,如 欢迎来到 C# 9.0 博客

This functionality takes away a lot of the boilerplate code necessary to create a simple C# application by not having to wrap your code in the usual namespace/class/Main method, as explained in the Welcome to C# 9.0 blog

创建一个简单的Hello World"应用程序唯一需要的顶级程序代码如下(摘自博客)

To create a simple "Hello World" application the only required code for a Top-level program is the following (taken from the blog)

using System;

Console.WriteLine("Hello World!");

为了试用此功能,我安装了在 Visual Studio 2019 (v16.6.5) 中运行的最新 .NET 5 预览包 (5.0.100-preview.6.20318.15) 并创建了以下正常"版本:从 VS 编译和运行的项目:

To try out this feature I have installed the latest .NET 5 preview package (5.0.100-preview.6.20318.15) running in Visual Studio 2019 (v16.6.5) and created the following "normal" project which compiles and runs from wihtin VS:

using System;

namespace TestProgram
{
    class Test
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world!");
            var fooBar = "Foo" + "bar";
            Console.WriteLine(fooBar);
            Console.ReadLine();
        }
    }
}

为了测试顶级程序,看看可以(不)做什么,我去掉了命名空间、类定义和 Main 方法:

To test out the Top-level program and see what could(n't) be done with it I got rid of the namespace, class definition and Main method:

using System;

Console.WriteLine("Hello world!"); // 1
var fooBar = "Foo" + "bar"; // 2
Console.WriteLine(fooBar); // 3
Console.ReadLine(); // 3

现在应该是有效的语法.这是项目中唯一的文件,据我所知,它符合该博客中提到的所有其他标准:

Which should now be valid syntax. This is the only file in the project and to my knowledge it conforms to all other criteria mentioned in that blog:

允许任何声明.程序必须在 using 之后和文件中的任何类型或命名空间声明之前发生,并且您只能在一个文件中执行此操作,就像今天您只能拥有一个 Main 方法一样.

Any statement is allowed. The program has to occur after the usings and before any type or namespace declarations in the file, and you can only do this in one file, just as you can have only one Main method today.

但是在实践中 VS 强调了所有错误,这些错误阻止我从 VS 中编译为发布或调试.

However in practise VS underlines everything with the errors preventing me from compiling as either release or debug from within VS.

(1) 命名空间不能直接包含字段或方法等成员
(2) 上下文关键字var"只能出现在局部变量声明或脚本代码中
(3)名称Console.WriteLine(/ReadLine)在当前上下文中不存在

(1) A namespace cannot direclty contain members such as fields or methods
(2) The contextual keyword 'var' may only appear within a local variable declaration or in script code
(3) The name Console.WriteLine(/ReadLine) does not exist in the current context

这是人们期望在 VS pre-.NET 5 中看到的,但是 .NET 5 肯定已启用,语言预览功能也是如此.如 .csproj 所示:

Which is what one would expect to see in VS pre-.NET 5, however .NET 5 is surely enabled, and so are language preview functions. As seen in the .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <LangVersion>preview</LangVersion>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

对我来说奇怪的是,当我尝试使用 dotnet build 从 CLI 进行编译时,程序会编译,并且可执行文件可以完美运行.

To me the odd thing is that when I try to compile from the CLI using dotnet build the program compiles, and the executable runs flawlessly.

Visual Studio 是否有错,它尚不支持此语法,还是我必须在某处启用某些功能才能使顶级程序成为一件事?

Is Visual Studio at fault here that it does not support this syntax yet, or do I have to enable something somewhere to make Top-level programs a thing?

推荐答案

总结意见,您必须使用 Visual Studio 的最新预览版(这很明显,因为 C# 9 现在处于预览状态)或构建您的来自控制台的应用程序(就像您已经使用 dotnet build 完成的一样).

Summarizing the comments, you have to use the latest preview version of Visual Studio (that's obvious, because the C# 9 is in preview state right now) or build your app from console (like you've already done with dotnet build).

您可以使用跟踪已实现功能的状态Roslyn 存储库中的语言功能状态页面.如您所见,顶级语句功能已合并到 16.7p3 分支中.我想这意味着该功能至少可以在 16.7 预览版 3 中使用(您确认它可以在 16.7.0 预览版 4.0 版中使用).

You can track the status of implemented features using Language Feature Status page in Roslyn repo. As you can see, top-level statements feature were merged into 16.7p3 branch. I suppose it means that feature will work in version 16.7 preview 3 at least (you confirmed that it works in 16.7.0 preview 4.0 version).

作为一种选择,您也可以尝试

As an option, you can also try sharplab.io for that feature, just switch a Roslyn branch on top right corner of left panel

这篇关于C# 9.0 支持 Visual Studio 2019 中的顶级程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆