简体   繁体   English

如何在 Visual Studio 2019 中更改 C# 版本

[英]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!如果有人可以提供帮助,那就太好了,谢谢!

Screenshot of the advanced build settings高级构建设置的屏幕截图

Taken directly from the documentation直接取自文档

The compiler determines a default based on these rules:编译器根据以下规则确定默认值:

Target framework目标框架 version版本 C# language version default C#语言版本默认
.NET .NET 7.x 7.x C# 11 C# 11
.NET .NET 6.x 6.x C# 10 C# 10
.NET .NET 5.x 5.x C# 9.0 C# 9.0
.NET Core .NET芯 3.x 3.x C# 8.0 C# 8.0
.NET Core .NET芯 2.x 2.x C# 7.3 C# 7.3
.NET Standard .NET 标准 2.1 2.1 C# 8.0 C# 8.0
.NET Standard .NET 标准 2.0 2.0 C# 7.3 C# 7.3
.NET Standard .NET 标准 1.x 1.x C# 7.3 C# 7.3
.NET Framework .NET 框架 all全部 C# 7.3 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.

相关问题 在 Visual Studio 2019 中更改 C# 版本 - Changing the C# version in Visual Studio 2019 Visual Studio 2019:C# 版本 - Visual Studio 2019: C# Version Visual Studio 2019 版本 c# 问题 - visual studio 2019 version c# probleme 如何有效地读取我的 C# 服务的 Visual Studio 2019 memory 快照? - How do I read a Visual Studio 2019 memory snapshot of my C# service usefully? 如何在 Visual Studio 2019 中添加 Google MyBusiness C# 客户端库 - How do I add the Google MyBusiness C# Client Library in Visual Studio 2019 如何在 Visual Studio 2005 或 2019 中为 .NET Framework 2.0 编译一个简单的 WinForms C# 应用程序? - How do I compile a simple WinForms C# application for .NET Framework 2.0 in Visual Studio 2005 or 2019? 在 Visual Studio 2019 / .NET Framework 4.8 中确定 C# 版本 - Determine C# version in Visual Studio 2019 / .NET Framework 4.8 Visual Studio 2019,C# 代码库,是否可以更改引用的名称? - Visual Studio 2019, C# codebase, Is it possible to change the name of a reference? 有没有办法在 Visual Studio 2019 中更改 C# 项目的字符集? - Is there a way to change character sets for C# projects in Visual Studio 2019? 如何将 Visual Studio 2019 中的 .NET 版本更改为 .NET Framework 4.7.2? - How to change .NET version in Visual Studio 2019 to .NET Framework 4.7.2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM