How to use C# 7 with Visual Studio 2015?

C#Visual StudioVisual Studio-2015C# 7.0

C# Problem Overview


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

How can I use C# 7 with it?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuis TeijonView Question on Stackoverflow
Solution 1 - C#Stephen KennedyView Answer on Stackoverflow
Solution 2 - C#yibeView Answer on Stackoverflow