Enabling C# 7 Compilation With Visual Studio 2017

You may be thinking that C# 7 features are already supported with Visual Studio 2017 and you do not need to make any changes for that. Yes, you are almost right but this is not 100% true.

Wait for a while and let me explain. If you open Visual Studio 2017 and start compiling the code which was introduced with C# 7 then most of the features will be compiled successfully and you do not need to make any changes for the language version. 

But if you will check the currently selected C# language version then you will find that it is “default” not “C#7.0”. Let’s check it.

  1. Open Solution Explorer inside Visual Studio 2017
  2. Select the project and right click on it to open the context menu and select “Properties”.

    C#
  1. Inside the Project property window click on “Build Tab” and the click on “Advanced…” button.

    C#

    It will open the “Advanced Build Settings” window.
  1. Inside the “Advanced Build Settings” window you can see that the selected language version is “default”.

    C#

    The great thing is that by selecting the “default” option we can compile most of the language features of C# 7.
  1. Now select the language version as C#7.0 and compile the code.

    C#

When you will compile the C# 7 code with this option then then you may get compilation error like:

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

But this error is not with all type of application. When I am compiling a console application with language version C# 7 then it is being compiled successfully whereas the web app is giving compile time error.

C#

So far you have seen the problem. Now you may be thinking, what is the solution? So the solution is very simple. You will need to install the latest version of Microsoft.Net.Compilers (version 2.0 or any upper version).

C#

After installing Microsoft.Net.Compilers your web app will compile successfully with language version C#7.0.

But still you may need some more Nuget packages depending on language features you are using e.g. you need to install “System.ValueTuple” for tuples.

For complete list of C# 7 features you can visit following article,

Up Next
    Ebook Download
    View all
    Learn
    View all