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.
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.
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.
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.
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.