[英]How do I change the C# version in Visual Studio 2019
I'm currently writing a project in C# 7.3 but I need to change it to C# 10.0我目前正在 C# 7.3 中编写一个项目,但我需要将其更改为 C# 10.0
When I go to my project's properties' advanced build settings, the option to change the language version is disabled.当我 go 到我的项目属性的高级构建设置时,更改语言版本的选项被禁用。 I'm not sure why it's grayed out, but I don't know how to manually change the C# version.
我不知道为什么它是灰色的,但我不知道如何手动更改 C# 版本。 If someone could help, that would be great, thanks!
如果有人可以提供帮助,那就太好了,谢谢!
Taken directly from the documentation直接取自文档
The compiler determines a default based on these rules:编译器根据以下规则确定默认值:
Target framework![]() |
version![]() |
C# language version default ![]() |
---|---|---|
.NET ![]() |
7.x ![]() |
C# 11 ![]() |
.NET ![]() |
6.x ![]() |
C# 10 ![]() |
.NET ![]() |
5.x ![]() |
C# 9.0 ![]() |
.NET Core ![]() |
3.x ![]() |
C# 8.0 ![]() |
.NET Core ![]() |
2.x ![]() |
C# 7.3 ![]() |
.NET Standard ![]() |
2.1 ![]() |
C# 8.0 ![]() |
.NET Standard ![]() |
2.0 ![]() |
C# 7.3 ![]() |
.NET Standard ![]() |
1.x ![]() |
C# 7.3 ![]() |
.NET Framework ![]() |
all![]() |
C# 7.3 ![]() |
C# 10 is supported only on .NET 6 and newer
仅 .NET 6 和更新版本支持 C# 10
You haven't provided the target framework for your project.您还没有为您的项目提供目标框架。 But you likely need to retarget your project to a framework that supports C#10
但是您可能需要将项目重新定位到支持 C#10 的框架
You can try to edit the.csproj file by adding something like this:您可以尝试通过添加以下内容来编辑 .csproj 文件:
<PropertyGroup>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
C#10 is already the default language version if your target framework is .NET 6, provided you're not overriding it with the LangVersion
entry as mentioned in other answers.如果您的目标框架是 .NET 6,C#10 已经是默认语言版本,前提是您没有像其他答案中提到的那样使用
LangVersion
条目覆盖它。 If you're not targeting .NET 6 or compiling with VS/Build Tools 2022, you'll need to upgrade those first in order to have proper C#10 support.如果您的目标不是 .NET 6 或使用 VS/Build Tools 2022 进行编译,则需要先升级它们才能获得适当的 C#10 支持。
If you are attempting to set the C# language version manually, keep in mind the default can be overridden in two locations (or programmatically ): the project file (aka ProjectName.csproj
), or a Directory.Build.props file (for explicitly overriding multiple projects at once).如果您尝试手动设置 C# 语言版本,请记住可以在两个位置(或以编程方式)覆盖默认值: 项目文件(又名
ProjectName.csproj
)或Directory.Build.props文件(用于显式覆盖一次多个项目)。 If the aforementioned props file exists, check it as well as your project file to ensure you don't have conflicting entries.如果上述道具文件存在,请检查它以及您的项目文件,以确保您没有冲突的条目。
In your case, there's really no reason to manually specify the language version;在您的情况下,确实没有理由手动指定语言版本。 just upgrade your compiler and use the default.
只需升级您的编译器并使用默认值。 If at that point you still find the project targeting anything < C#10, be aware that the culprit isn't necessarily confined to being the project file.
如果此时您仍然发现项目针对任何 < C#10,请注意罪魁祸首不一定限于项目文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.