Your Quick Guide to Pattern Matching in C#

Pattern Matching in C# - Featured Image

Last Updated on September 13, 2023 by Aram

Pattern matching in C# is a feature used to test expressions for some conditions while testing their types.

Generally, Pattern matching is a functional programming feature that already exists in other popular languages such as Scala, Rust, Python, Haskell, Prolog and many other languages.

Pattern Matching was introduced in C# 7, and ever since it has been receiving many updates over the subsequent major releases

This guide will help you learn more about the types of pattern matching with a usage example for each type.

Remember, you can only apply pattern matching within either ‘is’ expressions or switch expressions.

Benefits of Pattern matching in C#

  • Type-Testing
  • Nullable-type checking
  • Type casting and assignment
  • High readability
  • Concise syntax
  • Less convoluted code

So let’s get started with the different types of pattern matching in C#

C# 7

Type Pattern

Type-testing for the expression

Declaration Pattern

Type-testing as well as assignment to variable after a successful match of the expression

Constant Pattern

Testing versus a constant value which can include int, float, char, string, bool, enum, field declared with const, null

Null Pattern

Check if a reference or nullable type is null

Var Pattern

Similar to the type pattern, the var pattern matches an expression and checks for null, as well as assigns a value to the variable.

The var type is declared based on the matched expression’s compile-time type.


C# 8

Property Pattern

It is a highly usable type of pattern matching where you can incorporate object members rather than variables to match the given conditions.

It can be easily used with the other pattern matching types, such as the relative pattern, pattern combinators and many other patterns to build flexible and powerful logical expressions.

Usage of nested properties was available in C# 8 along with C# 9, but implementing it was not the cleanest solution or the most concise syntax.

Discard Pattern

Pattern matching with the discard operator _ matches anything including null, its use shines in the new switch expressions, to match the default or else case. In the below example, if the food’s storage temperature was not provided or the number doesn’t match the below ranges, then a custom exception will be thrown:

Positional Pattern

Mainly used with struct types, leverages a type’s deconstrutor to match a pattern according to the values position in the deconstructor

Notice we are also using the discard pattern to ignore the value of currency, since anything that has zero value, regardlress of the currency, should be free.

Tuple Pattern

A special derivation from the positional pattern where you can test multiple properties of a type in the same expression


C# 9

‘Enhanced’ Type Pattern

You can do type checking in switch expressions without using the discards with each type

Relational Pattern

Introduced in C# 9, a relational pattern allows applying the relational operators > < >= <= to match patterns versus constants or enum values

Logical Pattern

This represents the set of negation, conjunctive, disjunctive; not, and, or respectively. Together these are called the pattern combinators

These are used to combine patterns and apply logical conditions on them

Negated Null Constant Pattern

Checks expression for not null value

Parenthesized Pattern

This allows the use of parenthesis to control the order of execution and to group logical expressions together. It can be used with any type of pattern, but its usage is mainly associated with the use of pattern combinators


C# 10

Extended Property Pattern

In C# 10, the nested properties matching syntax issue was addressed.

With the introduction of the Extended Property Pattern, syntax to use nested properties in pattern matching is now clean and concise.


C# 11

List Pattern

The list matching pattern is the latest addition to the great set of pattern matching in C#, with list pattern you can match a list or an array with a set of sequential elements

You can mix it with discard, pattern combinators, range, var, type assignemnt patterns to build very flexible and powerful list pattern matching


Too Long; Didn’t Read

Pattern Matchingi in C#

Summary

During the recent few years, C# has been receiving lots of language features to help developers build solid and reliable application on a diverse range of platforms.

With the addition of the pattern matching features, C# has added a great functional programming capability that has been being used for decades throughout a multitude of programming languages.

Of course, you always need to remain cautious not to over-use the pattern matching.

Mainly if you are working on a large project with other team members, remember that not everyone would be aware about some or all of the pattern matching features, so you don’t want to suddenly introduce too many of these.

This article gave a quick and complete overview of all the pattern matching types added in C#.

Let me know if you found it useful and if you have any comment.

References

To learn more about Pattern Matching in C#, you can check these references:

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns

https://code-maze.com/csharp-pattern-matching/

.NET Learning Resources

As part of my learning journey I always try to assign some time to write and share content with anyone who is interested to learn.

Please feel free to connect with me LinkedIn, I do regularly share content mainly in .NET and C#.

Also you can always read any of my tutorials in this blog, here are some examples:

Collaborations and Sponsorships

If you want to learn about the available opportunities to collaborate and work together, please check this page:

https://codingsonata.com/sponsorships-and-collaborations/

Bonus

Enjoy the romantic piano tunes of the brilliant classical/romantic era music virtuoso, Frédéric Chopin.

Chopin, Waltz in A minor, B 150, Op. Posth

4 Comments on “Your Quick Guide to Pattern Matching in C#”

  1. Very nice article! One notice: var-pattern doesn’t check the null (o is var stillNullableValue), it does object-pattern (o is { } nonNullValue)

Leave a Reply