问题
With the coming of C# 9.0 and .NET 5 a new feature is getting introduced called "Top-level programs".
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
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!");
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();
}
}
}
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:
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.
However in practise VS underlines everything with the errors preventing me from compiling as either release or debug from within VS.
(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 nameConsole.WriteLine(/ReadLine)
does not exist in the current context
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>
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.
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?
回答1:
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
).
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
来源:https://stackoverflow.com/questions/62969728/c-sharp-9-0-support-for-top-level-programs-in-visual-studio-2019