list pattern matching in CSharp

With the release of C#10 and C#11 pattern matching was further enhanced to include the list pattern matching, enabling developers to deconstruct and analyze the array and collection.

Pattern matching in C# allows you to inspect the structure and values of an object cleanly and expressively.

What is List Pattern Matching?

List pattern matching allows the elements of a list or array to be matched declaratively. It allows you to track individual items, also the order of the collection, such as length and size in specific locations.

This is extremely useful when working with operations that need an element-based evaluation of collections, such as checking if a list starts with certain values or has a specific number of elements.

Syntax Overview

The syntax for list pattern matching uses square brackets [] to specify the pattern to match the collection elements. Here are some common uses:

  1. Matching the exact length of a list
  2. Extracting and deconstructing values from specific positions
  3. Matching a list that begins or ends with certain values

Examples

The following is an example of list pattern matching:

Matching an empty list

To check if a list is empty, you can use a simple pattern:

csharp
int[] numbers = new int[] { }; if (numbers is []) { Console.WriteLine("The list is empty."); }

Matching a list with a specific List

Here, you can match a list of a specific length by specifying placeholders: The pattern [_, _, _] matches any array with exactly three elements, regardless of the values.

csharp
int[] numbers = new int[] { 1, 2, 3 }; if (numbers is [_, _, _]) { Console.WriteLine("The list contains exactly 3 elements."); }

Matching the First and Last element

In this example, the pattern [1, ..] checks if the first element is 1, while .. ignores the rest of the elements, and The pattern [.., 3] matches lists that end with 3.

csharp
int[] numbers = new int[] { 1, 2, 3 }; if (numbers is [.., 3]) { Console.WriteLine("The list ends with 3."); } if (numbers is [1, .., 3]) { Console.WriteLine("The list starts with 1 and ends with 3."); } //We check both at one time using this syntax if (numbers is [1, .., 3]) { Console.WriteLine("The list starts with 1 and ends with 3."); }

Extracting Values with Deconstruction

In this example, the first and second elements are deconstructed and then stored in variables first and second.

csharp
int[] numbers = new int[] { 1, 2, 3 }; if (numbers is [var first, var second, ..]) { Console.WriteLine($"The first two elements are {first} and {second}."); }

Combining List Pattern Matching with Other Patterns

List pattern matching can also be combined with other types of patterns, such as type patterns and relation patterns.

Example

In this example, combining it with conditional checks for more complex scenarios:

int[] numbers = new int[] { 1, 2, 3 }; if (numbers is [>= 1, <= 2, ..]) { Console.WriteLine("First element is >= 1 and second is <= 2."); }

When to Use List Pattern Matching

  • Simplifying Code: It eliminates the need for manual for or foreach loops and makes your code cleaner and more declarative
  • Structural Checks: When it is important to validate or perform actions based on the structure of a list, such as ensuring lines are a certain length or start/end at a certain value.
  • Deconstruction: This makes it easy to extract values ​​from specific locations in the list.
line

Copyrights © 2024 letsupdateskills All rights reserved