Mastering Pattern Matching and Unique Logic in C#11 for Unparalleled Empowerment
Elevate your coding skills and ignite innovation with arrays and list patterns
As you know, Microsoft introduced pattern matching in version 7 of C#. But wait, C#11 brought new masterpieces to the table.
Microsoft evolved it into an important and influential control structure for everyday situations. List patterns allow patterns to be applied recursively to the individual elements of a list-like input — or at least a section.
On this basis, the new features of switch expressions offer never-seen heights of practical capabilities in checking for matches.
The Basis To Work With
I will use the following example, which will serve as a central illustration that will be further elaborated upon.
var array = new int[] { };
var match = array switch
{
_ => "No matchy matchy"
};
Console.WriteLine(match);
// Console -> No matchy matchy
The provided example is straightforward but a perfect starting ground for any experience level.
What does it do? It examines an integer array for a match and outputs the relevant message when required.