C# 11 Updates: List and Slice Patterns, More

C# Programming Guide

With Visual Studio 2022 17.1 being available, a lot of C# features have been added. Here are a few highlights, including list patterns, slice patterns, parameter null checking, and interpolated strings.

What are C# List Patterns and Slice Patterns?

A List pattern allows you to match an array of lists with a sequence of patterns. A slice pattern can only be used once and only in a list patterns clause. A list pattern is compatible with any type that is countable as well as indexable. This is because a list pattern has an accessible indexer. A slice pattern with a sub pattern is compatible with any type that is countable as well as sliceable. A small code example on its structure follows:

list_pattern_clause
  : '[' (pattern (',' pattern)* ','?)? ']'
  ;

list_pattern
  : list_pattern_clause simple_designation?
  ;

slice_pattern
  : '..' pattern?
  ;

primary_pattern
  : list_pattern
  | slice_pattern
  | 
  ;

For more information on list patterns, have a look here.

Here is some example code showing how to use list pattens in C#. The pattern [51, 52, .., 60] matches all of the following:

int[] arr1 = { 51, 52, 60 };
int[] arr1 = { 51, 52, 55, 60 };
int[] arr1 = { 51, 52, 55, 56,57, 58, 59, 60 };

Read: How to Use Dictionary Objects in C#

Parameter Null-checking in C#

C# developers are used to testing if method arguments are null in the following way:

public static void Method(string Arg)
{
    if (Arg is null)
    {
        throw new ArgumentNullException(nameof(Arg));
    }
}

With this preview feature of C# 11, the same can be accomplished by the following code:

public static void Method(string Arg!!)
{
    //Method Body
}

C# code will automatically be generated to perform the null tests. This code will execute before any of the code in the method body. In Constructors, the generated code will be performed before any field initialization.

You can also look into Nullable Reference Types (NRT) for more information.

Read: Best IDEs for .NET Developers

Interpolated Strings in C#

As C# programmers probably know, C# supports two types of interpolated strings. They are as follows.

Non-verbatim &@””

Non-verbatim interpolated strings cannot contain newlines and must always be used with escape characters such as \r and \n.

Verbatim $””

Verbatim interpolated strings are able to contain newlines and do not need to escape any characters within them.

The next code example demonstrates what would have resulted in an exception in C# 10, but not in C# 11:

var vCount = $"Count strTemp: { Something()
             Something.Else(1 + 2)[That.Can.Wrap()] }.";

There are a lot more changes expected in C# 11, so let’s see what Microsoft can come up with next!

Read more C# programming tutorials and guides.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read