137

Background

We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6.

We have no intention to use C# 6 within this particular solution (as much as I'd like to).

Problem

Visual Studio and ReSharper suggest helpful C# 6 language constructs that render the solution inoperable in earlier versions of Visual Studio without C#6 support.

I've disabled the ReSharper C#6 support but I can't seem to disable / limit C# features across the whole solution.

Question

How do I limit C# to C#5 capabilities within a solution or within Visual Studio 2015?

Community
  • 1
  • 1
SeanKilleen
  • 8,809
  • 17
  • 80
  • 133
  • 17
    Build tab, Advanced button, Language version setting. – Hans Passant May 26 '15 at 14:40
  • @HansPassant thanks! This is exactly it. I'll write it up in answer form but if you post the answer here, I'll be sure to mark yours as the correct one. – SeanKilleen May 26 '15 at 14:42
  • Exactly my problem! except our solution is in 2013, but I want to continue using VS2015 instead. – haku May 27 '15 at 14:56
  • @entre there's not necessarily a need; you can always just not *use* the C#6 features. Visual studio and resharper helpfully suggest refactorings that utilize these features, though. As long as your build machine supports C#6, it shouldn't be a problem. – SeanKilleen Jul 10 '15 at 10:21
  • I dont want to use c#6... but I am still getting those suggestions.. i removed them for resharper.. but asking for visual studio... i guess visual studio wont provide intellisense suggesting c#6 because my .net framework is 4.5 – harishr Jul 10 '15 at 11:03
  • Entire -- if you follow the directions in the answer, you'll be setting it for visual studio. You change the language from "default" to 5. It's in each project's properties – SeanKilleen Jul 10 '15 at 11:04
  • @SeanKilleen Our checkin build machine does not so we encountered the issue. Also some devs using VS2013 due to perf issues and bugs in VS2015 (regressions introduced by Roslyn) so I'm looking to disable C# 6.0 on a few solutions if possible. – Shiv Jun 20 '17 at 23:43

6 Answers6

137

You can set the language feature for each project separately by going to Properties => Build tab => Advanced button => Language Version and set your preferred version.

You should realize that it will still use the new "C# 6.0" .Net Compiler Platform (codenamed Roslyn). However, that compiler will imitate the behavior of older compilers and will limit you to features only available on that specific language version.


I don't think that there's a solution-wide setting available.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • 1
    Do you know if there is an equivalent setting for VB.NET? – Bradley Uffner Jul 16 '15 at 13:11
  • 2
    I got my answer earlier today. https://stackoverflow.com/questions/31454902/restrict-new-language-features-of-visual-basic-in-visual-studio-2015 – Bradley Uffner Jul 17 '15 at 01:45
  • 1
    After changes in Project properties my Studio just marks C# 6.0 code in editor. But doesn't treat it as error (builds successfully). – Eugene Sep 30 '15 at 11:20
  • 2
    If you have many projects, I recommend editing the csproj directly. Add the following inside the first `PropertyGroup` element: `5` – mhand Feb 03 '16 at 23:02
  • Works exactly the same way to disable C#7 support in VS2017 - simply use `6` instead. – Paul Suart Jun 12 '17 at 12:23
49

add below in .sln.DotSettings should disable it on solution level

<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>

Or if you don't have a .sln.DotSettings file:

  1. If your solution file is called Apple.sln, create a file beside it called Apple.sln.DotSettings.

  2. Give it the following contents:

    <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>
    </wpf:ResourceDictionary>
    
  3. Close and reopen the solution, Resharper should only warn you about C#5 things.

  4. Don't forget to remove this when you eventually start using C#6 features! :)

demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
harishr
  • 17,807
  • 9
  • 78
  • 125
  • 1
    This should be marked as the answer as it applies to all projects within a solution and is a file can be checked in. – twoleggedhorse Jan 27 '16 at 13:07
  • 8
    @twoleggedhorse actually this answer is only relevant to Resharper which OP already disabled, so it doesn't even answer the question. – i3arnon Feb 01 '16 at 09:35
  • 1
    I also agree that, this should be marked as answer. OP has asked about solution wide settings, which ONLY this answer provides. – sam-xor Jun 14 '16 at 08:55
  • 1
    Well, anyhow this does not prevent Visual Studio to show Microsoft's own recommendations for code improvements (it was the actual question to prevent those). This answer does work for ReSharper, not for Visual Studio (Not all of us are using ReSharper). – Stephen Reindl Dec 22 '16 at 15:31
11

This tool I wrote might help you if you have many projects that you need to set LangVersion for.

khellang
  • 17,550
  • 6
  • 64
  • 84
10

You can set the language feature for all the solutions/csproj with the MSBuildUserExtensionsPath.

Search the value of the $(MSBuildUserExtensionsPath), it should be something like C:\Users\$(User)\AppData\Local\Microsoft\MSBuild

Then edit the file Force.LangVersion.ImportBefore.props in the folder $(MSBuildUserExtensionsPath)\14.0\Imports\Microsoft.Common.Props\ImportBefore with :

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <LangVersion>5</LangVersion>
  </PropertyGroup>
</Project>
Ram
  • 3,092
  • 10
  • 40
  • 56
fhusson
  • 296
  • 4
  • 8
9

Steps have already been written above, just adding a screenshot further of my VS2015:

Properties of project >> Build >> Advanced >> Language version

enter image description here

I set that to C# 5.0.

Raghav
  • 8,772
  • 6
  • 82
  • 106
8

Right click on Project in Project Explorer and select Properties.

When the Properties tab opens select Build and the click the Advance button in bottom right.

There is drop-down box called Language Version. Change the select to "C# 5.0"

Nicknow
  • 7,154
  • 3
  • 22
  • 38