154

Visual Studio 2017 (15.x) supports C# 7, but what about Visual Studio 2015 (14.x)?

How can I use C# 7 with it?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Luis Teijon
  • 4,769
  • 7
  • 36
  • 57
  • 2
    refer this link: http://www.strathweb.com/2016/03/enabling-c-7-features-in-visual-studio-15-preview/ – Sunil Kumar Sep 13 '16 at 05:36
  • 8
    @SunilKumar: That's enabling them in VS 15 (which is VS 2017). It doesn't mention VS 2015 (which is VS 14) anywhere. – Jon Skeet Sep 13 '16 at 06:19

2 Answers2

168

You can replace the compiler shipped with Visual Studio for a C# 7-enabled version by installing the Nuget package Microsoft.Net.Compilers:

Referencing this package will cause the project to be built using the specific version of the C# and Visual Basic compilers contained in the package, as opposed to any system installed version.

There is no indication that I can see on the package page as to whether this is officially supported in Visual Studio 2015. My not-thorough tests so far indicate that it works but not painlessly - C# 7 code compiles, but is underlined with the red squiggly line which indicates a syntax error: ScreenshotNote that you will also need to install the Nuget package System.ValueTuple to use the new C# 7 value tuples features.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • 10
    In that case it would be better to use VIsual Studio Code, or simply install Visual Studio 2017 Community – Panagiotis Kanavos Mar 27 '17 at 14:13
  • 4
    This is a good thing to know. So you can slowly switch some projects to VS2017 knowing that _in case of emergency_ other developers _can_ work with your code. – ecth Apr 21 '17 at 08:17
  • 11
    Note while I agree in theory with @PanagiotisKanavos if your an employee of a medium or large company VS2017 Community Edition is likely not valid to be used as a licensee. I believe VS Code is true FOSS. – Chris Marisic May 23 '17 at 19:17
  • 1
    @PanagiotisKanavos VS Code does not support debugging applications running on the Desktop .NET Framework, says their [site](https://code.visualstudio.com/docs/languages/csharp). – tm1 Aug 28 '17 at 06:18
  • @PanagiotisKanavos, it seems that VisuaStudio 2015's **editor** could be “replaced” as well by mapping file types to be opened by a VS2017-era **VisualStudio Code** instead of by VisualStudio 2015's internal editor (which is effectively VS2015-era VisualStudio Code). [link](https://www.codeproject.com/articles/100117/how-to-change-visual-studio-default-editor) – Andreas ZUERCHER Oct 16 '17 at 16:58
  • 1
    There exists a use-case for updating VisualStudio 2015 to be as 2017-ish as possible: VisualStudio2015 & its Xamarin supports targeting Windows 8.1 and Windows Phone 8.1, whereas VisualStudio2017 does not. I will try this to see whether C#7-era/VS2017-era compiler from Microsoft.Net.Compilers Nuget (as well as the “replacement” of VS2015's editor with VisualStudio Code2017) works for performing Xamarin development for Windows 8.1 and Windows Phone 8.1. I'll post back in coming days (?) when I complete this task. – Andreas ZUERCHER Oct 16 '17 at 17:15
  • 7
    I installed the latest Microsoft.Net.Compilers nuget to my Xamarin.Forms UWP, Windows 8.1, and Windows Phone 8.1 projects, as well as the latest System.ValueTuple nuget (plus the latest revision of all the other System.* series nugets). Everything appears to work fine in the build and runtime. (I haven't tried every nook & cranny of the debugger's feature-set.) – Andreas ZUERCHER Oct 16 '17 at 19:52
  • 14
    VS2015.3, compiling for .NET 4.7.1: `Microsoft.Net.Compilers is only supported on MSBuild v15.0 and above` - I needed to use this version: https://www.nuget.org/packages/Microsoft.Net.Compilers/2.4.0 to build it, then it worked fine. :) – CAD bloke Jan 06 '18 at 22:37
  • 1
    This saved my sanity when VS2017 became unstable with the latest upgrade and wouldn't open my solution without crashing. It (VS2015) might say that the build failed, but the messages usually go away if you close all code windows. – Bruce Pierson Feb 01 '18 at 18:31
  • @CADbloke I've installed the package and the project have compiled successfully, but the syntax still remains with errors, but working fine – Ricardo Pontual Jun 05 '18 at 13:45
  • I'm getting a "A numeric comparison was attempted on "$(MSBuildVersion)" that evaluates to "" instead of a number, in condition "$(MSBuildVersion) >= 16.1.0" It fails to add the package. Not sure what to do about that? – vexe Sep 17 '19 at 15:25
6

In my case, installing just Microsoft.Net.Compilers didn't work. Instead, I had to do the following:

  1. Install Microsoft.CodeDom.Providers.DotNetCompilerPlatform for the project (Tools => NuGet Package Manager => Manage Nuget Packages for Solution...) and install Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
  2. Install the latest Microsoft.Net.Compilers for the project
  3. Install any other NuGet package for the latest C# feature you want to use. In my case, my goal was to use Tuples, so I installed System.ValueTuple and worked fine.

But still note that C# codes which are not known by Visual Studio 2015 default compiler will still have red squiggles underneath.

yibe
  • 338
  • 3
  • 7