简体   繁体   中英

Changing the C# version in Visual Studio 2019

I'm using visual studio 2019 and I'm trying to change my C# version. The reason I am doing this is that the build servers I use use an older version of VS / MSBuild to build and deploy code (this is outside my control). Therefore I need to use C# 5.

On previous versions of visual studio, you could do this from the menu in [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 previous question but it was lacking detail.

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 (eg $"{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.

The simplest way to change C# language version in Visual Studio 2019 is to:

  1. Right-click on a project in Solution Explorer (files tree) in Visual Studio;
  2. Then from the popup menu select "edit project item properties" -> "c# language level";
  3. On the list you will see all possible c# versions.

That's it! It works like a charm. Please note, that you should not do any other changes to the project files - so if you did any, please revert them to the original state. Also you don't need any <LangVersion> tags.

Right click on the project, go to Properties , then click on Build in left pane, select C# version you want to keep or just select latest major version. 在此处输入图像描述

While saving, your {projectname}.csproj file's LangVersion property will be updated.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM