Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# 7.1 with MSBuild

To use the new C# 7.1 language features with Visual Studio 2017, you add the setting <LangVersion>latest</LangVersion> to your project file(s).

However, building such projects from MSBuild (version 15.3.409.57025, located at C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin) results in an error:

CSC : error CS1617: Invalid option 'latest' for /langversion;
must be ISO-1, ISO-2, Default or an integer in range 1 to 6.

Is this feature just not yet supported by MSBuild, or is it possible to get this working?

This covers 200+ projects that were originally created variously in Visual Studio 2013 and 2015. They were all re-targeted to .NET 4.7 using the Target Framework Migrator tool (which saved lots of clicking and appears - based on inspecting .csproj file changes - to do the job correctly).

The projects all build successfully from Visual Studio 2017.

like image 862
Richard Ev Avatar asked Sep 26 '17 07:09

Richard Ev


People also ask

What is using () in C#?

The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.

How do I start learning C?

Get started with C. Official C documentation - Might be hard to follow and understand for beginners. Visit official C Programming documentation. Write a lot of C programming code - The only way you can learn programming is by writing a lot of code.

How do I practice C programming?

Begin with solving basic level logical questions, and while solving always follow the best approach and best practices. Show activity on this post. A really good way to exercise C is to read (or reread) The C Programming Language (K&R) and to do every exercise at the end of each chapter.


2 Answers

Nuget packages

  • Microsoft.Net.Compilers nuget package does not work and needn't to be installed.

Set the following project/build settings

  • Set at least C# 7.1 or higher in the Debug and Release build properties. (via: Project menu > [ProjectName] Properties > Build tab > [Advanced] button > Language Version).

  • Setting it to latest does not work.

preview

Also make sure that you are running the latest MSBuild version.

like image 152
Dominic Jonas Avatar answered Oct 17 '22 03:10

Dominic Jonas


Add a reference to the Microsoft.Net.Compilers package (version 2.3.x, to get C# 7.1).

A short description of the package:

Microsoft.Net.Compilers

This package not only includes the C# and Visual Basic compilers, it also modifies MSBuild targets so that the included compiler versions are used rather than any system-installed versions. Once installed, this package requires Microsoft Build Tools 2015.

like image 8
Julien Couvreur Avatar answered Oct 17 '22 02:10

Julien Couvreur