Introducing C#11: List Pattern
Introduction
With C# 11, it is possible to match an array or list that matches certain elements. It is possible to check all the elements of an array or a list, which implicitly makes possible to check the length of an array and the position of these items in the collection.
The power of List pattern
C# 11 allows using the discard _, which symbolizes any element, and the Range pattern …, which can specify a range of items (from 0 to n) regardless of their value. C# 11 allows (and it’s excellent) a combination of the discard and the range pattern.
The following snippet shows two arrays of characters; an array of consonants and an array of vowels. The snippet also shows matching operations using the is operator that can be made on these arrays:
It’s particularly useful when it comes to check if an array or a list corresponds to particular expectations. Do you like it ? 😊