Enabling C# 7.1 and 7.2 features in your .NET code
January 16, 2018 Leave a comment
C# 7.0 has been quickly enhanced with a couple of new features in 7.1 and 7.2. If you start Visual Studio 2017 and try e.g. the new “private protected” access modifier then the compiler may not accept it:
public abstract class Animal { private String privateName = "Private name"; protected String protectedName = "Protected name"; protected internal String protectedInternalName = "Protected internal name"; private protected String privateProtectedName = "Private protected name"; public String publicName = "Public name"; }
If it puts a red line under “private protected” saying that this access modifier combination is not allowed then you’ll know that C# 7.2 has not been enabled yet.
First of all make sure that you have the latest .NET SDK installed on your computer: .NET SDKs for Visual Studio . This is 4.7.1 at the time of writing this post.
Next select the project properties, go to the Build tab and click the Advanced… button:
It opens the build settings window where you can specify the language version in the “Language version” drop down list. By default it will say “C# latest major version (default)”. This is where the problem lies as it resolves to C# 7.0 due to the “major” specification of the option. Change it to “C# latest minor version (latest)”:
Alternatively you’ll be able to select one of the specific language versions such as “C# 7.1” as well.