C# 11 is on the way! Microsoft Releases +5 New Features
Tags: csharp, dotnet, beginners, news
Published: 2022-05-10 08:38:00

Just a couple of days ago Microsoft announced and revealed the new features of .NET 7. All very focused on pure performance up to **[73.5% faster but what about C#? Are there new features and new things? Are they focusing on performance too? If you're reading means yes and without further ado, let's take a look at what's new in the new C# 11 features!

---

## Generic attributes

The first new feature is related to generic classes. Now you will be able to use `GenericAttribute` to declare generic classes when the base class of the generic class is `System.Attribute.`

This way, `GenericAttribute` will allow us to have a **better syntax** if the attribute requires parameters of type System.Type because before it was done this way:

```csharp
// Before C# 11:
public class TypeAttribute : Attribute
{
    public TypeAttribute(Type t) => ParamType = t;
    public Type ParamType { get; } 
}
```

Now Microsoft, with this new feature, **simplifies the work for developers** by allowing us to do it in [the following way](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#):

```csharp
// C# 11 feature: 
public class GenericAttribute<T> : Attribute { }
// Specify parameter type
[GenericAttribute<string>()] public string Method() => default;
```

As a warning, Microsoft reminds us that **not all types of arguments are allowed**. [According to Microsoft](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#:~:text=These%20types%20aren't%20directly%20represented%20in%20metadata.%20They%20include%20annotations%20that%20describe%20the%20type.%20In%20all%20cases,%20you%20can%20use%20the%20underlying%20type%20instead:), the allowed ones we can use are the following:

- `object` for `dynamic`.
- `IntPtr` instead of `nint` or `unint`.
- `string` instead of `string?`.
- `ValueTuple<int, int>` instead of `(int X, int Y)`.

---

## New Raw string literals

This new feature is an enhancement of [string literals](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#string-literals). Microsoft has now added a new format called _**[raw string literals](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals)**_.

As Microsoft says, this new format **allows different types of special characters without needing to escape them**. Apart from special characters, it also allows:

- Arbitrary text
- Embedded quotes
- New lines
- Whitespaces

The main feature of raw string literals is that they always open with (at least) 3 double-quotes and must close with the same number of double-quotes. With this practical example Microsoft shows us how to use it:

```csharp
string longMessage = """
     This is a long message.
     It has several lines.
         Some are indented
                 more than others.
     Some should start at the first column.
     Some have "quoted text" in them.
     """;
```

In addition, Microsoft reminds us that we can [**combine string interpolation with _raw string literals_**](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals) by adding multiple dollar symbols `$$` .

To learn more about this feature I recommend you to consult the [original post from Microsoft](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals).

---

## Extended List patterns

[List patterns](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns) in this update of C# 11 allows to have a wider matching. It is now possible to **match any pattern with sequences of elements** (both in an _array_ and in a _list_).
This applies to:
- Property patterns
- Constant patterns
- Relational patterns
- Type patterns

Then there is the **discard pattern** `_`. This pattern has the ability to match any single element.

To see it in a more visual way, let's use [Microsoft's example](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#list-patterns):

```csharp
Console.WriteLine(odd is [1, _, _]); // true 
Console.WriteLine(odd is [_, 3, _]); // true
```

There is also the new **range pattern** `..` . This pattern can be used to match any sequence of more than zero elements (including zero).

Here again, let's see [how Microsoft uses it in its example](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#list-patterns):

```csharp
Console.WriteLine(odd is [1, .., 3, _]); // true Console.WriteLine(fib is [1, .., 3, _]); // true
```

📚 If you want to go deeper into the Patterns, I leave here the original Microsoft article: **[C# 11 List Patterns](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#list-patterns)**

---

## Newlines in string interpolations

Previously, the text of a string interpolation `{text}` allowed only one line. Now in C# 11 this text can allow more than one line.

For those who do not know, this is the structure of an item with an [interpolation expression](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated):

```csharp
{<interpolationExpression>[,<alignment>][:<formatString>]}
```

The main advantages of this feature is to have a clearer reading and compression of string interpolations (especially those using long expressions).

---

## Method Group conversion to Delegate (updated)

Previously, in versions of the standard, when creating a delegate object for a method group conversion, the compiler could not reuse it.

Now, [as Microsoft explains](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#improved-method-group-conversion-to-delegate):

> "The conversion is permitted (but not required) to use an existing delegate instance that already contains these references."

To understand what happens, the compiler of the new version of C# has the ability to **reuse the delegate object** by storing it previously in cache.

---

## Static Abstract Members in Interfaces

Thanks to this new feature we can **add static members in the interfaces**. These static members can contain other static members, overloadable operators and static properties.

Microsoft tells us that their main use is mathematical operators on generic types. Besides, the [Nuget](https://www.nuget.org/packages/System.Runtime.Experimental/) `packageSystem.Runtime.Experimental` has been added just for mathematical operations, which is much appreciated :)

If you want to know how it works, as always I recommend the original Microsoft source: [Explore static abstract interface members](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/static-abstract-interface-methods)

As we already know, **the estimated date of C# 11 is in November of this year**. There are still many months of development ahead from Microsoft and we will be waiting for any news from them. Let's see what else will surprise us!