C# 11 is Coming! 5 Features that will Blow your Mind 🤯
Tags: csharp, dotnet, news, beginners
Published: 2022-02-28 11:27:00

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](https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-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:](https://github.com/dotnet/csharplang/issues/4935)

- 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](https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/#c-11-preview-allow-newlines-in-the-holes-of-interpolated-strings) of what could be done with C# 11 that now, with C# 10 would not be possible because it would give error:

```csharp
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**](https://twitter.com/kathleendollard) 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:

```csharp
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:

```csharp
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.](https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-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:

```csharp
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 `!!`:

```csharp
public static void M(string s!!)
{
    // Body of the method
}
```
This could also be used in checking indexer parameters with `get` and `set`:

```csharp
public string this[string key!!] { get { ... } set { ... } }
```

---

## Constructors

In this feature there are a [few small changes](https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/#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.

---

## 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](https://devblogs.microsoft.com/dotnet/early-peek-at-csharp-11-features/#interaction-with-nullable-reference-types):

```csharp
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.