12

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.

Armand Bernard
  • 449
  • 1
  • 3
  • 12
  • Have you tried the Directory.Build.props route described [here](https://www.meziantou.net/4-ways-to-enable-the-latest-csharp-features.htm) ? – Caius Jard Feb 16 '20 at 09:38
  • 1
    (examines project **Properties.Build.Advanced** and notices it is now `grayed out`! [Tell me more](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version)) Thanks, I learnt something new today. The sooner .NET 5 gets here the better. Though for all we know we'll all be forced to use YAML :( –  Feb 16 '20 at 09:39
  • 2
    What framework version are you on? Have you checked that there isn't another `LangVersion` element further down the file? What arguments are you passing to MSBuild on your build server? There might be something that overrides the settings in the csproj. – Lennart Feb 16 '20 at 09:39
  • 1
    ps; is 5 a valid value for this element? Have you tried 5.0? – Caius Jard Feb 16 '20 at 09:40
  • Did this declaration `latest` help you? – Pavel Anikhouski Feb 16 '20 at 15:10
  • How have you verified "it ignores it"? If you crank up the build verbosity, what's the command line used to build? If I do this with a fresh project file and add a `LangVersion` block, I get a `CoreCompile:` step that includes a `csc.exe` invocation that uses `/langversion:5`. In other words, this option gets passed down to the compiler, no problem. You may have incorrect expectations of what this option *does* -- it only restricts the constructs allowed in the source, it does not ensure the project or the build output is compatible with earlier versions of the .NET Framework or VS itself. – Jeroen Mostert Feb 17 '20 at 12:24
  • If the option is in effect and used by VS, code of the form `string s = null; int x = s?.Length;` will produce an error, as the null-conditional operator was introduced in C# 6. – Jeroen Mostert Feb 17 '20 at 12:25
  • @JeroenMostert changing the C# version ensures that incompatible code is marked as such, which means I know what changes to make for it to compile on older VS versions. This is not in relation to .NET version. – Armand Bernard Feb 17 '20 at 21:33
  • @PavelAnikhouski I'm not looking for the latest version, but an older one, so no. – Armand Bernard Feb 17 '20 at 21:33
  • @CaiusJard turns out both work now that I've fixed the underlying issue. I've added this to my solution – Armand Bernard Feb 17 '20 at 21:33

4 Answers4

8

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.

Armand Bernard
  • 449
  • 1
  • 3
  • 12
2

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. enter image description here

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

enter image description here

Rohil Patel
  • 386
  • 3
  • 8
1

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.

Marcin
  • 479
  • 6
  • 11
1

If Visual Studio does not allow you to change the language version, you need to proceed as follows, by editing the project file:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

I had the same problem. I added the following line to section: 7.3