In this post, we will be looking at C# 11 New Features with the help of code snippets.
These features are available in the C# preview version and C# 11 is not yet officially released at the time of writing this post.
C# is an object-oriented, type-safe, and managed language that is compiled by .NET Framework to generate Microsoft Intermediate Language (MSIL).
Interested to know how C# Evolved?
Check 👉 C# Version History
The most recent version of the C# programming language is C#10. We have written a detailed post covering C# 10 features.
Topics Covered
C# 11 New Features
The preview features for C# 11 are included in Visual Studio 17.1 and .NET SDK 6.0.200. You may update Visual Studio or download the latest .NET SDK to use these features.
These are the C# 11 new features that we will be looking at:
- “Holes” in interpolated strings
- Constructors
- Parameter Null checking
- Nullable Reference Types
- List patterns
“Holes” in interpolated strings
C# currently supports two types of interpolated strings:
- Verbatim interpolated: $@””
- Non-verbatim interpolated: $””
The main difference between the two is that:
- Verbatim interpolated strings can contain new lines of code in their text segments and can only escape a proper quotation mark ” “.
- This does not happen in non-verbatim interpolated strings; in these cases escape characters (such as /r/n) are used.
For example, the following would have resulted in a compiler error in C# 10 and is valid in this C# 11 preview:
var v = $"Count ist: { this.Is.Really.Something() .That.I.Should( be + able)[ to.Wrap()] }.";
Holes are a shorthand way of saying interpolation expressions and are the portions inside the curly braces that supply runtime values.
Constructors
There is a small change with respect to the constructors. If at any time an explicit null check change is performed with the !! (null validation syntax), that validation will occur after the field initializers.
Before any of these, null-checks using the parameter null-check syntax will be done.
Parameter Null checking
Microsoft has put this feature in this preview to ensure they have time to get the feedback.
It is quite common to validate if the method arguments are null like this:
public static void M(string s) { if (s is null) { throw new ArgumentNullException(nameof(s)); } // Body of the method }
With Parameter null checking, we can minimize the additional lines of code:
public static void M(string s!!) { // Body of the method }
The parameter null checking cannot be used on:
- extern method parameters.
- Delegate parameters.
- Interface method parameters when the method is not a Default Interface Method (DIM).
Nullable Reference Types (NRT)
If we apply !! operator to the name of a parameter, it will start as non-null with a nullable state.
void WarnCase( string? name!!, // CS8995 Nullable type 'string?' is null-checked and will throw if null. T value1!! // Okay )
As we can see, the compiler gives a warning when !! syntax on parameters is used with an explicitly nullable type on the parameter.
List patterns
List patterns allow us to compare with arrays and lists, being able to match different elements or even, include a cut pattern that matches zero or more elements. Let’s understand this with the help of an example by Kathleen from Microsoft.
The pattern [1, 2, .., 10] matches all of the following:
int[] arr1 = { 1, 2, 10 }; int[] arr1 = { 1, 2, 5, 10 }; int[] arr1 = { 1, 2, 5, 6, 7, 8, 9, 10 };
To explore list patterns consider:
public static int CheckSwitch(int[] values) => values switch { [1, 2, .., 10] => 1, [1, 2] => 2, [1, _] => 3, [1, ..] => 4, [..] => 50 };
When it is passed the following arrays, the results are as indicated:
WriteLine(CheckSwitch(new[] { 1, 2, 10 })); // prints 1 WriteLine(CheckSwitch(new[] { 1, 2, 7, 3, 3, 10 })); // prints 1 WriteLine(CheckSwitch(new[] { 1, 2 })); // prints 2 WriteLine(CheckSwitch(new[] { 1, 3 })); // prints 3 WriteLine(CheckSwitch(new[] { 1, 3, 5 })); // prints 4 WriteLine(CheckSwitch(new[] { 2, 5, 6, 7 })); // prints 50
C# 11 New Features – Youtube video
You may refer to our youtube video that explains 5 C# 11 New Features in the preview version.
C# Course Recommendation
One of the best courses to master your C# skills right from level zero. This series is divided into 4 sections:
- C# Basics
- C# Intermediate
- C# Advanced
- Unit Testing for C# Developers
Further Reading
Hope you liked the C# 11 New Features and would give them a try. Feel free to share these with your friends and colleagues.
PS: If you found this content valuable and want to return the favor, then
Disclaimer: Most of the content is FREE. But some resources may contain affiliate links. When you purchase, I may receive a commission from the seller, at no extra cost to you.
Happy Coding {}