DEV Community

Cover image for C# 11 is Coming! 5 Features that will Blow your Mind 🤯
ByteHide
ByteHide

Posted on • Edited on • Originally published at bytehide.com

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

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()] }.";
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

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
    };
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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

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

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

public string this[string key!!] { get { ... } set { ... } }
Enter fullscreen mode Exit fullscreen mode

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
)
Enter fullscreen mode Exit fullscreen mode

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.

Latest comments (32)

Collapse
 
andagr profile image
Andreas Ågren

☕☕☕☕

Collapse
 
fireal1983 profile image
Fireal

☕☕☕☕☕☕

Collapse
 
lajoskvcs profile image
Lajos Kovács

☕☕☕☕☕☕☕
I feel so sorry my little hearth 🥲

Collapse
 
stimocea profile image
stimocea

☕☕☕

Collapse
 
artoo profile image
Arto

☕🍵🍵

Collapse
 
kswaroop1 profile image
kswaroop1

Ok'ish, but real innovation seems lacking. Better to concentrate on tail recursion, discriminated unions/sum types, pipe operator. These will go a long way in reducing amount of code needed to express domain. Rust has it.

C# has been very good at introducing language features while taking the devs along a gently upward sloping curve. Lots of functional programming features there already, might as well completely support it now. Those who do not want to use it, can ignore these features.

Alternately, allow mixed language projects such that both C# and F# language files can reside in same project.

Collapse
 
gozaro profile image
Gozaro

☕☕☕

Collapse
 
omertrans678 profile image
omertrans678

☕ brown = new ☕();

Collapse
 
marcibme profile image
marcibme

☕🥐☕

Collapse
 
bytehide profile image
ByteHide

Caffeine + Carbohydrates + Caffeine = 🤙😎

Collapse
 
steveplays profile image
Steveplays

I like tea more :D
Not a regular tea or coffee drinker though lol

Good article!

Collapse
 
bytehide profile image
ByteHide

And what type of tea do you prefer? Green, black...? I am not an expert in tea 😔

Thanks for your time :D

Collapse
 
steveplays profile image
Steveplays

I like all kinds of tea really, haha
Also, you're welcome!