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

Simply a few days in the past Microsoft introduced and revealed the brand new options of .NET 7. All very centered on pure efficiency (as much as 73.5% quicker) however what about C#? Are there new options and new issues? Are they specializing in efficiency too? Should you’re studying means sure and with out additional ado, let’s check out what’s new within the new C# 11 options!

Generic attributes

The primary new function is expounded to generic lessons. Now it is possible for you to to make use of GenericAttribute to declare generic lessons when the bottom class of the generic class is System.Attribute.

This manner, GenericAttribute will permit us to have a higher syntax if the attribute requires parameters of kind System.Sort as a result of earlier than it was accomplished this fashion:

// Earlier than C# 11:
public class TypeAttribute : Attribute
    public TypeAttribute(Sort t) => ParamType = t;
    public Sort ParamType { get; } 
Enter fullscreen mode

Exit fullscreen mode

Now Microsoft, with this new function, simplifies the work for builders by permitting us to do it in the following way:

// C# 11 function: 
public class GenericAttribute<T> : Attribute { }
// Specify parameter kind
[GenericAttribute<string>()] public string Methodology() => default;
Enter fullscreen mode

Exit fullscreen mode

As a warning, Microsoft reminds us that not all forms of arguments are allowed. According to Microsoft, the allowed ones we will use are the next:

  • object for dynamic.
  • IntPtr as a substitute of nint or unint.
  • string as a substitute of string?.
  • ValueTuple<int, int> as a substitute of (int X, int Y).

New Uncooked string literals

This new function is an enhancement of string literals. Microsoft has now added a brand new format referred to as raw string literals.

As Microsoft says, this new format permits various kinds of particular characters with no need to flee them. Aside from particular characters, it additionally permits:

  • Arbitrary textual content
  • Embedded quotes
  • New strains
  • Whitespaces

The principle function of uncooked string literals is that they at all times open with (a minimum of) 3 double-quotes and should shut with the identical variety of double-quotes. With this sensible instance Microsoft exhibits us how you can use it:

string longMessage = """
     This is a lengthy message.
     It has a number of strains.
         Some are indented
                 extra than others.
     Some ought to begin at the first column.
     Some have "quoted textual content" in them.
Enter fullscreen mode

Exit fullscreen mode

As well as, Microsoft reminds us that we will combine string interpolation with raw string literals by including a number of greenback symbols $$ .

To study extra about this function I like to recommend you to seek the advice of the original post from Microsoft.

Prolonged Checklist patterns

List patterns on this replace of C# 11 permits to have a wider matching. It’s now doable to match any sample with sequences of components (each in an array and in a checklist).
This is applicable to:

  • Property patterns
  • Fixed patterns
  • Relational patterns
  • Sort patterns

Then there may be the discard sample _. This sample has the flexibility to match any single component.

To see it in a extra visible manner, 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’s additionally the brand new vary sample .. . This sample can be utilized to match any sequence of greater than zero components (together with zero).

Right here once more, let’s have a look at 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 wish to go deeper into the Patterns, I go away right here the unique Microsoft article: C# 11 List Patterns

Newlines in string interpolations

Beforehand, the textual content of a string interpolation {textual content} allowed just one line. Now in C# 11 this textual content can permit multiple line.

For individuals who have no idea, that is the construction of an merchandise with an interpolation expression:

Enter fullscreen mode

Exit fullscreen mode

The principle benefits of this function is to have a clearer studying and compression of string interpolations (particularly these utilizing lengthy expressions).

Methodology Group conversion to Delegate (up to date)

Beforehand, in variations of the usual, when making a delegate object for a way group conversion, the compiler couldn’t reuse it.

Now, as Microsoft explains:

“The conversion is permitted (however not required) to make use of an current delegate occasion that already accommodates these references.”

To know what occurs, the compiler of the brand new model of C# has the flexibility to reuse the delegate object by storing it beforehand in cache.

Static Summary Members in Interfaces

Because of this new function we will add static members within the interfaces. These static members can include different static members, overloadable operators and static properties.

Microsoft tells us that their principal use is mathematical operators on generic sorts. Apart from, the Nuget packageSystem.Runtime.Experimental has been added only for mathematical operations, which is way appreciated 🙂

If you wish to know the way it works, as at all times I like to recommend the unique Microsoft supply: Explore static abstract interface members

As we already know, the estimated date of C# 11 is in November of this yr. There are nonetheless many months of improvement forward from Microsoft and we shall be ready for any information from them. Let’s examine what else will shock us!

Add a Comment

Your email address will not be published. Required fields are marked *