Search code examples
c#visual-studio-2019

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.


Solution

  • 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.