Skip to content

C# 11 is Coming! 5 Features that will Blow your Mind 🤯

28 de February de 2022
csharp 11 new features

Microsoft recently released C# 10 and .NET 6 to the world, but they aren’t done yet! The .NET team has already started working on features that will be included in C# 11 and .NET 7. In this article, we’ll take a look at some of the upcoming C# 11 features that will change how you code forever!

Index

“Holes” in interpolated chains

To introduce this new feature that C# 11 will bring, we have to keep in mind that C# currently supports two types of intepolated strings:

  • Verbatim interpolated: $@""
  • Non-verbatim interpolated: $""

The main difference here 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.

When I mention “holes”, I — and Microsoft — mean interpolation expressions.

All this affected (and still affects) all the “holes” in the non-verbatim interpolated strings. Since these holes are not really text, they should not be affected by the escape rules.

Let’s see Microsoft’s example of what could be done with C# 11 that now, with C# 10 would not be possible because it would give error:

List patterns

Here is another new feature: the new list pattern. What it allows us to do in C#11 is to compare with arrays and lists, being able to match different elements or even, to include a cut pattern that matches zero or more elements.

As Kathleen tells us, the slice pattern can go after, for example, another list pattern, such as the var pattern, in order to capture the contents of the slice.

Let’s look at Microsoft’s example:

The pattern [1, 2, .., 10] matches all of the following:

To explore list patterns consider:

You can see the example in more depth in the Early peek at C# 11 features.

Parameter Null Checking

This new feature is based on, as we already know, it is common to use variations of boilerplate code to validate if the method arguments are null, for example:

And now we can abbreviate the intention to check null parameters with !!:

This could also be used in checking indexer parameters with get and set:

Constructors

In this feature there are a few small changes. 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.

Interaction with Nullable Reference Types

If we apply to the name of a parameter the !! operator we have seen before, it will start as non-null with a nullable state. Let’s check Microsoft example:

As we can see, the compiler gives a warning when !! syntax on parameters is used with an explicitly nullable type on the parameter.

C# 11 conclusion

As only a couple of months ago (November last year), Microsoft officially released .NET 6 and C# 10, we could say that the new features and features of C# 11 are many, most of them have not been fully exploited and we will have to wait for Microsoft to talk about them in depth in the not too distant future.

Settings