DEV Community

Cover image for C# 11 is on the way! Microsoft Releases +5 New Features
ByteHide
ByteHide

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

C# 11 is on the way! Microsoft Releases +5 New Features

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:

// Before C# 11:
public class TypeAttribute : Attribute
{
    public TypeAttribute(Type t) => ParamType = t;
    public Type ParamType { get; } 
}
Enter fullscreen mode Exit fullscreen mode

Now Microsoft, with this new feature, simplifies the work for developers by allowing us to do it in the following way:

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

As a warning, Microsoft reminds us that not all types of arguments are allowed. According to Microsoft, 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. Microsoft has now added a new format called 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:

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

In addition, Microsoft reminds us that we can combine string interpolation with raw string literals by adding multiple dollar symbols $$ .

To learn more about this feature I recommend you to consult the original post from Microsoft.


Extended List patterns

List 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:

Console.WriteLine(odd is [1, _, _]); // true 
Console.WriteLine(odd is [_, 3, _]); // true
Enter fullscreen mode Exit fullscreen mode

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:

Console.WriteLine(odd is [1, .., 3, _]); // true Console.WriteLine(fib is [1, .., 3, _]); // true
Enter fullscreen mode Exit fullscreen mode

📚 If you want to go deeper into the Patterns, I leave here the original Microsoft article: C# 11 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:

{<interpolationExpression>[,<alignment>][:<formatString>]}
Enter fullscreen mode Exit fullscreen mode

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:

"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 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

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!

Top comments (0)