How To Enable C# 7.1 Version To My Projects

By default C# 7.0 was shipped With Visual Studio 2017 and this is the major release (March 2017) till now. In August 2017 a minor version of C# 7.0 was released and version named as C# 7.1. This minor version came with four new features (async main, target-typed default literals, tuple name inference, and generic support for pattern-matching) and some minor changes to the what was introduced in C# 7.0.

By default C# 7.0 was shipped with Visual Studio 2017 and this was the major release (March 2017) until now. In August 2017 a minor version of C# 7.0 was released, named C# 7.1. This minor version came with four new features (async main, target-typed default literals, tuple name inference, and generic support for pattern-matching) and some minor changes to what was introduced in C# 7.0.

Here, in this article, we will discuss how to make sure your existing projects point to the new C# language i.e., C# 7.1. To do this we have three approaches.

Approach 1 - Using Project Properties 

In this approach, go to your existing project where you want to point  to C# 7.1 and open project properties by right clicking on the project. Select properties in Visual Studio Solution Explorer window as below. 

How To Enable C# 7.1 Version To My Projects

Once the properties window opens, go to Build menu and click on Advanced button. Once this button is clicked a popup window will appear where you can see a language version drop down where we need to select C# 7.1 as shown below. 

As noticed in this dropdown, we find an option

C# latest major version (default)

When we select this option the runtime will take the latest major release available, in our case C# 7.0, and in the future, if there is a C# 8.0 release, this project will automatically point to the C# 8.0 compiler without any changes.

C# latest minor version (latest)

This is something different from the above, when we select this option the runtime will take the latest minor release, in our case C# 7.1  and in the future, if there is a C# 7.2 release, this project will automatically point to the C# 7.2 compiler without any changes.

If we select any other option, even if there are any major or minor changes, runtime won't consider those and will take only the compiler version which you provide here.

Once you select C# 7.1 from this drop-down, click on OK and save the project. Now your project will run on C# 7.1 compiler and you can use these new features.

Approach 2 - Using Project File 

In this approach, we need to explicitly edit the project's project file; i.e.., csproj file. In case of .NET Core or .NET Standard projects, we can directly edit using Visual Studios by right clicking and selecting Edit [Projectname].csproj option on a project in solution explorer window as below. 

How To Enable C# 7.1 Version To My Projects

If you are using anything other than .NET core or .NET standard projects, you need to go explicitly to the file location and edit the csproj file by right clicking and opening in any text editor tools like Notepad, EditPlus, Notepad++ etc..,.

Once this file is opened, we need to <LangVersion>,  add an element as a child element to <PropertyGroup> and provide C# 7.1 as the element text as follows.

  1. <PropertyGroup>  
  2.     <OutputType>Exe</OutputType>  
  3.     <TargetFramework>netcoreapp2.0</TargetFramework>  
  4.     <LangVersion>7.1</LangVersion>  
  5. </PropertyGroup> 

Make sure to add this element for each build configuration if you are having multiple build configurations for your project.

Approach 3 - Using Lightbulb Box  

This is my favorite approach, as by using this approach we can change the target language version to C# 7.1 for all the projects at once. As with the above two approaches, we need to manually change each project.

Try to implement any feature which is specific to C# 7.1, for example, we will try to implement Target-Typed Default literal by assigning default keyword to the integer variable. Once compiler identifies this, it will pop up a Lightbox code fix window and gives following options

  • Upgrade this project to C#language version 'latest'
  • Upgrade this project to C#language version '1'
  • Upgrade all C#projects to language version 'latest'
  • Upgrade all C#projects to language version '1'

Once you select any of the above options, Visual Studio will do all for you.

How To Enable C# 7.1 Version To My Projects

I hope this article provided you with a good bit of information about how existing projects can be pointed to the C# 7.1 language version.