155

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

How can I use C# 7 with it?

2
  • 2
    refer this link: strathweb.com/2016/03/… Commented Sep 13, 2016 at 5: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. Commented Sep 13, 2016 at 6:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

In that case it would be better to use VIsual Studio Code, or simply install Visual Studio 2017 Community
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.
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.
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.)
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: nuget.org/packages/Microsoft.Net.Compilers/2.4.0 to build it, then it worked fine. :)
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.