在 Visual Studio 2019 中更改 C# 版本

我正在使用 Visual Studio 2019,并且正在尝试更改我的 C# 版本。我这样做的原因是我使用的构建服务器使用旧版本的 VS / MSBuild 来构建和部署代码(这不在我的控制范围内)。因此我需要使用 C# 5。

在以前版本的 Visual Studio 中,您可以从 [Project] -> Properties -> Build -> Advanced. For VS2019 Microsoft, in their infinite wisdom, has decided to make this harder. Apparently you need to edit the project file manually and add:

<PropertyGroup>
   <LangVersion>[some version here]</LangVersion>
</PropertyGroup>

To your project file manually. That's all well and good, but I can't seem to get that working. It just ignores it, even after I unload and reload it. Here is a snapshot of my project file:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
   <LangVersion>5</LangVersion>
  </PropertyGroup>
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{58FE5465-851B-471F-B6A9-9C861FA5C022}</ProjectGuid>
    <OutputType>Library</OutputType>
...

Any idea how I can make this work? It's probably something really silly I'm missing. Note: I did see this 上一个问题的菜单中执行此操作,但缺少详细信息。

stack overflow Changing the C# version in Visual Studio 2019
原文答案
author avatar

接受的答案

Turns out @lennart was right: There were some other <LangVersion>'s in the file. VS had snuck them into some of the build configurations.

    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>TRACE;DEBUG;PLATFORM_X86</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <LangVersion>7.3</LangVersion> <-- HERE!!
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <OutputPath>bin\x86\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>

SOLUTION: I deleted all these configuration special cases and it defaulted back to the language I had listed at the top of the file.

I can tell it worked in my case because it now rejects interpolated strings (e.g. $"{value}words") saying it's not available in C#5.

As for what language names work, both "5" and "5.0" worked for me. The rest of the options can be found here

I feel kind of dumb now, I hope this question will still be useful to some future people.


答案:

作者头像

在Visual Studio 2019中更改C#语言版本的最简单方法是:

1.右键单击Visual Studio中的解决方案资源管理器(文件树)中的项目;
2.然后从弹出菜单中选择“编辑项目项目属性” - >“ C#语言级别”;
3.在列表中,您将看到所有可能的C#版本。

而已!它像魅力一样工作。请注意,您不应该对项目文件进行任何其他更改 - 因此,如果您这样做,请将其恢复为原始状态。另外,您不需要任何标签。

作者头像

右键单击项目,转到 Properties,然后单击左窗格中的 Build,选择要保留的 C# 版本或仅选择最新的主要版本。 enter image description here

保存时,您的 {projectname}.csproj 文件的 LangVersion 属性将被更新。

enter image description here