C# 11 is Coming! 5 Features that will Blow your Mind šŸ¤Æ

Juan EspaƱa
ByteHide
Published in
4 min readFeb 28, 2022

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!

ā€œ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:

var v = $"Count ist: { this.Is.Really.Something()
.That.I.Should(
be + able)[
to.Wrap()] }.";

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:

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
};

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:

public static void M(string s)
{
if (s is null)
{
throw new ArgumentNullException(nameof(s));
}
// Body of the method
}

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

public static void M(string s!!)
{
// Body of the method
}

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

public string this[string key!!] { get { ... } 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:

void WarnCase<T>(
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.

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.

If you liked this article, donā€™t forget to FOLLOW US, so that you can be one of the first to read whatā€™s new in .NET.

And if you are reading this, it means that you belong to the 1% of the people who read the articles UNTIL THE END, tell me how many coffees ā˜• you need per day to continue programming, if I see many coffees ā˜• I will not feel alone and you will make my day!!! šŸ˜ƒšŸ‘

--

--

Juan EspaƱa
ByteHide
Editor for

CEO at ByteHidešŸ”, passionate about highly scalable technology businesses and .NET content creator šŸ‘Øā€šŸ’»