How to upgrade msbuild to C# 6?

Anna Prosvetova picture Anna Prosvetova · Aug 14, 2015 · Viewed 56.1k times · Source

I want to use C# 6 in my project (null propagation, other features).

I've installed VS 2015 on my PC and it works brilliantly and builds test code like

var user = new SingleUserModel(); //all model fields are null
var test = user.User?.Avatar?["blah"];

But when I push my project to the repo and CI starts to build it, build fails because of unsupported ?.

I've installed VS2015 on CI server too but looke like it doesn't use it. What can I do?

CI - CruiseControl .NET Builds with C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe

Answer

jessehouwing picture jessehouwing · Aug 14, 2015

Make sure you call:

C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe

That's the version of MsBuild that ships with Visual Studio 2015 and calls the C# compiler that understands this. You can get this version of MsBuild on your system by installing any edition of Visual Studio 2015 or by installing the stand-alone Microsoft Build Tools 2015.

Adding a reference to the following NuGet package will also force use of the new compiler:

Install-Package Microsoft.Net.Compilers

Please note Install-Package will pick the latest available version which may not be the one you are looking for. Before you install, please check the release notes and dependencies to resolve the underlying issue with the version being dealt with, which in this case, was more specific to VS 2015.

So for Visual Studio 2015:

Install-Package Microsoft.Net.Compilers -Version 1.0.0