Examining objects with pattern matching
Pattern matching is a way to check an object’s value or the value of a property having a full or partial match to a sequence. This is supported in C# in the form of if…else
and switch…case
statements. In modern languages, especially in functional programming languages such as F#, there is advanced support for pattern matching. With C# 7.0, new pattern matching concepts were introduced. They are enhanced further in later versions of C#, that is, 8.0 and 9.0.
Pattern matching provides a different way to express conditions to have more-human- readable code. To understand pattern matching, we will be covering concepts from versions 7.0 and 8.0 as well. Let’s dig deeper into pattern matching in the following sections.
The constant pattern
Constant pattern matching examines the object’s value against a constant. With constant pattern matching, we will be asserting an object’s value against a constant...