C# version 11: a complete guide

C# is one of the recommended platforms for programmers to create powerful and secure programs that run on the .NET framework. Having its roots in the C family, this C# language has introduced some excellent features in every release and assisted developers in meeting various needs while bespoke software development.

C# 11 is the latest version of C# which is in development mode. We have seen the solid design principles of C# versions so far. Now, the C# 11 would be released with some advanced features that will do the developer’s task much easy.

Here are some features that are expected to come with the new version of C#.

C# 11 new features

  • Generic attributes
  • List patterns
  • Newlines in string interpolation expression
  • Static abstract members in interfaces
  • Improved method group conversion to delegate
  • Raw string literals

Generic Attributes

You can declare a general class whose base class is System.Attribute. This provides a more convenient syntax for features that require the System.Type parameter. First, you need to create an attribute that takes the type as its constructor parameter:

// Before C# 11:
public class TypeAttr : Attribute
{
   public TypeAttr(Type t) => ParamType = t;

   public Type ParamType { get; }
}

And to apply the attribute, you utilize the typeof operator:

[TypeAttribute(typeof(string))]
public string Method() => default;

Using this new feature, you can make a generic attribute instead:

// C# 11 feature:
public class GenericAttribute<T> : Attribute { }

After that specify the type parameter to use the attribute:

[GenericAttribute<string>()]
public string Method() => default;

When you apply in other words you must provide all the type parameters, the generic type must be completely constructed.

public class GenericType<T>
{
   [GenericAttribute<T>()] // Not allowed! generic attributes must be completely constructed types.
   public string Method() => default;
}

Type arguments must contain the same restrictions as typeof operator. Types that require metadata annotation are not allowed. For example, the following types are not allowed as type parameters:

  • dynamic
  • nint, unint
  • string? (any nullable reference type)
  • (int x, int y) (any other tuple types using C# tuple syntax).

These types are not displayed directly in the metadata. They include kind of descriptive remarks. In all cases, you can use the following type instead:

  • object for dynamic
  • intPtr instead of nint or unint
  • string instead of string?
  • valueTuple instead of (int x, int y).

List patterns

List Pattern Extends pattern matching to match the order of elements in a list or array. For example, the sequence [1, 2, 3] is true when the sequence is an array or a list of three integers (1, 2, and 3). You can match elements using any pattern, including consistency, type, property, and relative patterns. The delete pattern (_) matches any one component, and the new series pattern (..) matches any sequence of zero or more components. 

Newlines in string interpolation expression

The text inside { and } characters for a string interpolation can now span multiple lines. The text between { and } markers is parsed as C#. Any legal C#, including newlines, is allowed. This feature makes it easier to read string interpolations that use longer C# expressions, such as pattern matching switch expressions, or LINQ queries.

You can refer more about this new feature in the string interpolation article.

Static abstract members in interfaces

You can add static abstract members to the interface to define an interface that includes overloadable operators, other static members, and static properties. The primary scenario for this feature is to use mathematical operators in common types. The .NET runtime team has included an interface for mathematical operations in the System.Runtime.Experimental NuGet package. For example, you can implement System.IAdditionOperators in a way that executes Operator +. Other interfaces describe other mathematical operations or well-defined values.

To learn more, you can take an in-depth look at static abstract interface members

Constructor

There is a small change regarding the constructors. If at any time an explicit null check change is made with the !! (null validation syntax), that validation will take place after the field initializers.

Prior to any of these, null checks will be performed using the null-check-syntax parameter.

Improved method group conversion to delegate

Now C# standards on Method group conversion contains the following item:

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

Earlier versions of the standard prohibited the compiler from reusing the delegate object created for method group conversion. C # 11 The compiler method caches the delegate object created from group conversion and reuses the single delegate object.

Raw string literals

Raw String Literals is a new format for string literals. The raw string literally can contain arbitrary text as well as whitespace, new lines, embedded quotes, and other special characters without the need for escape sequences. Raw strings literally start with at least three double-quote (“””) characters and end with the same number of double-quote characters. Typically, a raw string uses three double quotes on one line to start a string, and three double quotes on a separate line to end the string. The new line after the opening quote and the front line of the ending quote are not included in the final content:

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

Any whitespace to the left of the closing double quote will be removed from the string. Raw string literals can be combined with string interpolation to include parentheses in the output text. Multiple 6 characters indicate how many consecutive brackets start and end the interpolation:

var location = $$"""
   You are at {{{Longitude}}, {{Latitude}}}
   """;

The next example illustrates that the two brackets start and ends with interpolation. The output string consists of a third repeating opening and closing brace.

Conclusion

In this article, we covered the upcoming new features of C# 11. We have seen in detail on generic attributes, list patterns, new lines in string interpolation expression, static abstract members in interfaces, constructor, improved method group conversion to delegate, and raw string literals. These changes would be more helpful for C# developers while web development. In fact, there may be more new features to come in the stable release of C# 11.

I am working as a Digital Marketing Manager in a reputed Outlook Addin Development Company name as iFour Technolab. Being a technical writing enthusiast and a goal-oriented individual with honed communication skills, I have served in the Information technology, Services, and Product industry. Having a high-energy level, I absolutely love what I do and I’m passionate about being better every day.

5 thoughts on “C# version 11: a complete guide

  • Oh my goodness! an amazing article dude. Thank you However I’m experiencing difficulty with ur rss . Don抰 know why Unable to subscribe to it. Is there anybody getting an identical rss drawback? Anyone who is aware of kindly respond. Thnkx

  • I needed to write you that very small remark to be able to give many thanks again for your pleasing strategies you have featured in this article. It is really extremely generous with people like you to make openly precisely what many individuals would have marketed as an electronic book to make some profit on their own, primarily since you could possibly have done it in the event you desired. These guidelines in addition worked as the easy way to know that most people have the identical dream just like my very own to realize a little more with regards to this matter. I believe there are some more pleasant opportunities up front for people who go through your blog post.

  • Needed to compose you this tiny note to be able to give many thanks the moment again with the exceptional guidelines you have shown on this page. This is so incredibly open-handed of people like you to provide unhampered just what most people could have marketed as an ebook to earn some money on their own, certainly considering that you could have tried it in case you desired. Those tips in addition worked to become good way to know that someone else have the identical eagerness the same as my very own to grasp whole lot more with regard to this condition. I’m certain there are many more pleasurable situations in the future for people who scan your website.

  • I happen to be writing to let you know of the helpful experience my daughter obtained using your site. She learned a wide variety of things, which include how it is like to possess an amazing teaching style to get men and women smoothly have an understanding of selected impossible issues. You actually did more than readers’ expectations. Many thanks for providing the valuable, trustworthy, educational and also easy tips about that topic to Janet.

Leave a Reply to jordan 6