DEV Community

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

Posted on • Updated 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.

Top comments (32)

Collapse
 
lidiaaadotnet profile image
lidiaaadotnet
while (true)
{

}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bytehide profile image
ByteHide

Hey yooo, wait a minute.

Collapse
 
lidiaaadotnet profile image
lidiaaadotnet

I'm waiting 1 day

Collapse
 
stefxr profile image
GalaxyStef

☕☕☕🥃

Collapse
 
bytehide profile image
ByteHide

I don't know what could go wrong with that combination😎

Collapse
 
stefxr profile image
GalaxyStef

Well, it helps with the abstract parts of the project 😊

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!

Collapse
 
wilfredoad profile image
wilfredoAD

0

Collapse
 
bytehide profile image
ByteHide • Edited

Do you prefer tea?🍵

Collapse
 
patricktingen profile image
Patrick Tingen

☕☕☕☕

Collapse
 
bytehide profile image
ByteHide

When should we consider coffee overdose? (open question)

Collapse
 
patricktingen profile image
Patrick Tingen

Overdose is when ☕ / ⌚=🤢

Collapse
 
jamesburton profile image
James Burton

☕☕☕

Collapse
 
bytehide profile image
ByteHide

Perfect! I think that amount is the balance between staying asleep and overdosing haha

Collapse
 
dumboprogrammer profile image
Tawhid

☕☕☕☕

Collapse
 
vamsea2002 profile image
vamsea2002

☕☕☕☕

Collapse
 
bytehide profile image
ByteHide

4 during the day or 4 at once?

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
 
marcibme profile image
marcibme

☕🥐☕

Collapse
 
bytehide profile image
ByteHide

Caffeine + Carbohydrates + Caffeine = 🤙😎

Collapse
 
sack251 profile image
sack251

☕☕☕/☕☕☕☕☕

Collapse
 
bytehide profile image
ByteHide

I like that range!☕

Collapse
 
andagr profile image
Andreas Ågren

☕☕☕☕

Collapse
 
lajoskvcs profile image
Lajos Kovács

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

Collapse
 
artoo profile image
Arto

☕🍵🍵

Collapse
 
gozaro profile image
Gozaro

☕☕☕