How to Use C# 7 With Visual Studio 2015

How to use C# 7 with Visual Studio 2015?

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.

C# 7.0 in Visual Studio Enterprise 2015

Yes, 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:

Screenshot Note that you will also need to install the Nuget package System.ValueTuple to use the new C# 7 value tuples features.

Visual Studio 2015 does not recognize C# 7.0 Syntax

To be able to use the new C# 7 language features, you need a C# compiler that understands these features.

VS.NET 2017 has a compiler that supports these features. The C# compiler incorporated in VS.NET 2015 does not.

How can I enable all features of C# 7 in Visual Studio 2017 project?

For arbitrary task-like types you linked to in the 2nd part of your question you need to include the System.Threading.Tasks.Extensions package.

The reason you need these NuGet packages is because the new language features rely on new types added to the .NET framework. The new types that the C# language features depend on will not be "built in to the framework" until the next minor version released after 4.6.2 to not break SemVer1. So unless you are building a project using that next version of the framework you will need to use the NuGet packages to get the features to work.

This is no different than getting extension methods to work in a .NET 2.0 project. You can use extension methods but you need to use a NuGet package (or add the code yourself) to get the types it relies on to be added to your project.


1: So 4.7 or 5.0, whatever they decide to call it, if there is a 4.6.3 it will not be in that version because that is not a minor release version bump, that is a patch version bump and you can't make API changes in a patch version bump without violating Semantic Versioning.

How do I enable C# 7 builds in Team Foundation Server 2015?

I didn't follow these steps exactly, but based on what I have learned, I think they would have worked:

  1. Install Visual Studio 2017 Build Tools on the XAML Build Configuration server
  2. Add these lines to the TFSBuildServiceHost.exe.config file:
  <configSections>
<section name="msbuildToolsets" type="Microsoft.Build.Evaluation.ToolsetConfigurationSection, Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />
</configSections>
<msbuildToolsets>
<toolset toolsVersion="15.0">
<property name="MSBuildToolsPath" value="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin" />
</toolset>
<toolset toolsVersion="latest">
<property name="MSBuildToolsPath" value="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin" />
</toolset>
</msbuildToolsets>

C:\Program Files\Microsoft Team Foundation Server
14.0\Tools\TFSBuildServiceHost.exe.config

(I never would have figure that out without this
post. Thank you jonesy2488!)


  1. Restart the XAML Build Configuration service.
  2. Change the XAML Build Process Template to include the ToolVersion="15.0" parameter:
<mtba:RunMSBuild DisplayName="Run MSBuild" OutputLocation="[OutputLocation]" CleanBuild="[CleanBuild]" CommandLineArguments="[String.Format("/p:SkipInvalidConfigurations=true {0}", AdvancedBuildSettings.GetValue(Of String)("MSBuildArguments", String.Empty))]" ConfigurationsToBuild="[ConfigurationsToBuild]" ProjectsToBuild="[ProjectsToBuild]" ToolVersion="15.0" ToolPlatform="[AdvancedBuildSettings.GetValue(Of String)("MSBuildPlatform", "Auto")]" RunCodeAnalysis="[AdvancedBuildSettings.GetValue(Of String)("RunCodeAnalysis", "AsConfigured")]" />

Code builds on vs 2017 but not in vs 2015

The specific feature you are using for your get definition (expression-bodied members) is specific to C# version 7.0, as detailed here.

So the reason why your code is not compiling is because VS 2015 uses C# 6.0 and VS 2017 uses C# 7.0.

You can change you get declaration to the following in order to make it compatible with C# 6.0 and the it will build in VS 2015:

get { return _frequency; }

Although I have never tried it, after a quick look around the web, it seems it would be possible for you to use C# 7.0 with Visual Studio 2015 if you would prefer that option. Then you should in theory be able to compile your code without making any changes.

How can I use C# 8 with Visual Studio 2017?

Going forward, Microsoft want to tie C# language versions more closely to framework versions than they have in the past. They really only want you to be using C# 8 with .NET Core 3.x and .NET Standard 2.1 projects, and that means using Visual Studio 2019. My answer to Does C# 8 support the .NET Framework? has all the gory details.

However, if you really want to you can now use C# 8 in Visual Studio 2017 by using the same trick that brings C# 7 to Visual Studio 2015: install the latest version of the Microsoft.Net.Compilers Nuget package into the project. It works, but of course VS 2017 doesn't know about C# 8 syntax so it doesn't look very pretty. Here's a screenshot showing that VS 2017 is able to compile a small test library using nullable reference types and a static local method (both of which are C# 8 features):

Sample Image


Here's the .csproj and code if you want to try it:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net452</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

-

using System;

namespace CSharp8Test
{
public class Class1
{
public string? NullableString { get; } = "Test";

public static void Test()
{
Console.WriteLine(Test2());
static int Test2() => 5;
}
}
}


Related Topics



Leave a reply



Submit