C# 11 - List Patterns
C# 11 - List Patterns

C# 11 – Introducing List Patterns Matching

Since past few articles, I have been trying to write about the new features introduced in C# 11 and .NET 7. We already have seen some features like raw string literals, static abstract members in interfaces, etc.

In this short article, we are going to have a look at list patterns matching.

What is this feature about ?

Starting from C# 11, array or lists can be matched with sequence of elements.

So, let’s consider the example given below.

  • There is one array containing few numbers (Fibonacci series)
  • Then there is a conditional statement which is a probably different from what we have seen in the past. The if statement is using a ‘is‘ keyword and it is trying to match the Fibonacci series array with another list which is a comma separated list of numbers, specified between two square brackets.

The comparison would return true only if every element is present in both lists at exactly the same index. Below code example and its output should help us understand this easily.

List Pattern Matching – Various Ways

As per documentation, there are three different ways for list pattern matching.

  • Discard pattern
  • Range pattern
  • Var pattern

Let’s try to understand what these patterns mean.

Discard Pattern

This pattern can be helpful to match one or more elements from a sequence, provided we know the length of the sequence.

So, let’s say we want to figure out if a number, 1, is present at first position in a sequence of 5 elements. In that case, the sequence to be used in if condition should be [ 1 , _, _, _ , _ ], as shown in the code snippet given below.

As shown in the above code snippet, you can specify one or more elements for list pattern matching. The only constraint is you need to know the length of the sequence you are comparing with. Otherwise comparison would always return false.

Range Pattern

If the length of sequence to be compares is not known, then range pattern may be useful. Two dots can be used to specify that any number of elements may be present in place of those two dots. Note that two dots can be used only once in the sequence.

The code snippet given below shows few examples of range patterns.

The pattern that we have used is a constant pattern, which means we have directly used the numbers in the sequence. We also have option to use relational pattern – which means, we can specify the comparison expressions as shown in code snippet given below.

The var Pattern

In this pattern, we can specify var keyword followed by the variable name. This means than the pattern is to capture the element present at that position. This variable can then be used in the same scope for whatever purpose you need it for.

In the code snippet given below, there are two cases. In first IF…ELSE block, the lists do match and the variable values are then printed to console. In second IF…ELSE blocks, the lists do not match.

Where list patterns are useful ?

This list pattern matching feature may prove handy for comparing the data from CSV files or fixed length column files. If the data is not present in all the columns, then list pattern matching is going to be helpful as per documentation.

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply