16 November

C# 11 – List patterns

Programming

min. read

Reading Time: 2 minutes
C# 11
C# 11

Let’s list all the features that C# 11 brings to the table. Let’s discuss them all one by one, with their use cases, and see how they can come in handy. At the bottom of each article, you can find a link to all 14 new C# features!

List patterns

Even though this feature is called list patterns, it also applies to arrays. So let’s start with that A list pattern will try to do an exact match of the provided array or a list. Let’s see an example.

int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers is [1, 2, 3]); // True
Console.WriteLine(numbers is [1, 2, 4]); // False
Console.WriteLine(numbers is [1, 2, 3, 4]); //False

List patterns with expressions

This functionality is also provided with expressions. All of the conditional expressions that you know.

You can do named conditions like or. Comparisons >=10, ==40

int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers is [0 or 1, <= 2, >= 3]); // True

Deconstruction

With those patterns, you can also make deconstruction to variables, which included “expand” deconstruction with ..

int[] numbers = { 1, 2, 3, 4 };
if(numbers is [var first, _, .. var others])
{
   Console.WriteLine(first); // 1
   Console.WriteLine(string.Join(", ", others.Select(x => x.ToString()))); 
   // 3, 4
}

Switch statements

You can also pattern match via switch statements. This can be useful to determine the state of the array if it has one or multiple elements.

var emptyArray = Array.Empty<string>();
var myName = new[] { "Tomasz Juszczak" };
var myNameFields = new[] { "Tom", "Juszczak" };
var myNameMiddleFields = new[] { "Tomasz", "Middle", "Juszczak" };

void SwitchStatement(string[] items)
{
   var text = items switch
   {
      [] => "Name was empty",
      [var fullName] => $"My name is {fullName}",
      [var firstName, var secondName] => $"My name is {firstName} {secondName}",
   };
   Console.WriteLine(text);
}

SwitchStatement(emptyArray); // Name was empty
SwitchStatement(myName); // My name is Tomasz Juszczak
SwitchStatement(myNameFields); // My name is Tom Juszczak
SwitchStatement(myNameMiddleFields); // SwitchExpressionException

This code will probably suggest to you that there the switch is not exhaustive and does not match all 3 elements of the array. To fix that we can add an “expand” operator at the end.

   var text = items switch
   {
      [] => "Name was empty",
      [var fullName] => $"My name is {fullName}",
      [var firstName, var secondName] => $"My name is {firstName} {secondName}",
      [.. var other] => $"My name is {string.Join(" ", other)}",
   };

Further reading at dotnet GitHub page.


Let's talk

SEND THE EMAIL

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.