I have a simple test console app. The only code it has is
public class Class1
{
private protected int _value = 0b_1001_0110;
}
which is just to test whether these C# 7.2 features will build or not.
In Visual Studio 2017 15.5.2 they build fine if I set the language version like this
If I now check the project into VSTS and run a build it fails with various errors, e.g.
Test\CSharp72Test\CSharp72Test\Class1.cs(7,17): Error CS0107: More than one protection modifier
Test\CSharp72Test\CSharp72Test\Class1.cs(7,40): Error CS1013: Invalid number
because it knows nothing about C#7.
If I now add 'Microsoft.NET.Compilers 2.6.1' NuGet package and rebuild it works fine locally but fails in VSTS with
Test\CSharp72Test\CSharp72Test\Class1.cs(7,40): Error CS8107: Feature 'leading digit separator' is not available in C# 7.0. Please use language version 7.2 or greater.
It's now using the correct version of CSC.exe but not passing /langversion:latest.
How do I get this passed correctly per project in a multi project solution?
The csc.exe executable file is usually located in the Microsoft.NET\Framework\<Version> folder under the Windows directory.
To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.
Visual Studio Build task is also calling MSBuild.exe
command to run the build.
You could directly pass MSBuild Argument and build succeed through the command line locally such as below:
msbuild "C:\Users\Admin\Source\repos\ClassLibrary2\ClassLibrary2\ClassLibrary2.csproj" /property:langversion=latest
So you just need to add /property:langversion=latest
in MsBuild Argument of the Visual Studio Build task in VSTS or directly change the value of <LangVersion>xx</LangVersion>
to latest in the project file.
However, according to VSTS Hosted VS2017 image, the host agent only have Visual Studio 2017 Enterprise * Version: 15.4.0 installed.
C# 7.2 ships with the 15.5 release of Visual Studio 2017.
There also have been a related user voice : Hosted agent with VS 15.5 to enable C# 7.2 support
As a workaround, you could set up your owner build agent and with Visual Studio 2017 15.5.2 installed.
You need to set the language version to C# latest minor version (latest)
for all build configurations not just Debug
. See here for how to do it.
Right-click
YourProject
, clickProperties
Click
Build
if it's not already selectedChange
Configuration
toAll Configurations
Click
Advanced...
Change the language version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With