How to Enable C# 7.1 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.
Here, in this article will discuss more on how to make sure your existing projects pointing to 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 this to C# 7.1 and open project properties by right click on the project and select properties in Visual Studio Solution Explorer window as below.
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 latest major release available in our case C# 7.0 and in future, if there is a C# 8.0 release, this project automatically points to 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 latest minor release in our case C# 7.1 and in future, if there is a C# 7.2 release, this project automatically points to C# 7.2 compiler without any changes.
If we select any other option, even though any major or minor changes, runtime won’t consider those and takes only compiler version which you provided 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 doing right click and select Edit [Projectname].csproj option on a project in solution explorer window as below.
If you are using other than .NET core or .NET standard projects, you need to go explicitly to the file location and edit the csproj file by right click and open 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> the element and provide C# 7.1 as the element text as follows.
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> <LangVersion>7.1</LangVersion> </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 using this approach we can change target language version to C# 7.1 for all the projects at once. As with 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 ‘7.1’
- Upgrade all C# projects to language version ‘latest’
- Upgrade all C# projects to language version ‘7.1’
Once you select any of the above options, Visual Studio will do all for you.
Hope this article provided you a good bit of information about how should existing projects can be pointed to C# 7.1 language version.
Happy Coding 🙂