C# Pattern Matching

C# is a nineteen year old language, it was introduced in 2001. Over the 19 years, this language experience ten version upgraded and Microsoft add some helpful features in each version. In C#7, there is a feature called pattern matching was introduced. Pattern matching successfully concise the code length, enhance code readability and reduce bug. Therefore, this feature is one of the favour feature of C# developers. In this post, I will share the idea of C# pattern matching by some practical cases. Since  this feature was introduced after C#6, more and more extended features about pattern matching was added in C#8 and C#9. It seems that this feature is an important feature in C# language now.

What is Pattern matching?

Pattern matching is a test expression  for the occurrence of given pattern.  It is a boolean operation that tell the developer that the variable meets the specific pattern or not.  Most of the case you implement in pattern matching that can be done in other ways too but that result in longer syntax.

Pattern matching supports the following  statement.

Is expression

The most practical situation of using "is expression" in pattern matching is type checking. The below picture is the example of type checking.

The above code test the variable is null or not. This is the most common usage case of is expression. 

Beginning at C#7, you can use type checking and assign the value to variable when the given condition is true by the below expression.

The code expression improves the code readability and concise the code length.Before C#7, developer must separate the condition checking and variable assignment in code of two lines.


Switch Statement

Switch statement is one of the traditional syntax to select the specified instruction to whose match with the condition.

Before C#7, the switch statement only supports the primitive type like int, char, float and string. However, beginning at C#7, switch expression can compare type and object. The below code expressions show you that the switch statement can evaluate type checking beyond the primitive type.  I create a method called PrintMessage which print different message based on the type of input parameter.


Console output different greeting message based on the type of input parameter.

And the function invoking show as follow.

Switch Expression

Switch expression is a feature which was introduced in C# 8.  We can use this feature to simplify the variable declaration with multiple condition. Before, this feature was added, we have to write a switch statement to implement the conditional assignment. But now, developers can write the statement in the below format. Here is the example of switch expression.

And the below picture is the console output of the above statement.

Compared with the traditional statement, the code is more concise and  similar to the modern language style like "Go" and "Rust".  In this example,  _ variable is a discard variable which replaces the origin default keyword.  Discard variable is an important feature which was introduced in C#7 too. But now, all you need to know is that it represents default value in the above switch expression.

Relation Pattern and Case Guard

Before  I  summarize this article, I would like to introduce two more patterns which were introduced in C#9. They are relation pattern and case guard.

Relation Pattern

It compares the variable with constant  value. The result is same as if else statement but the code style is more concise.  The below code expression is the relation pattern expression.   The express compares the input value with constant and return the right message to describe the numerical value of the number

Case Guard

The final pattern is case guard. It is an advance pattern matching for sophisticated developers. In some conditional expression , the condition must be satisfy as the same time . But the switch case statement cannot support this kind of operation before C#9. The following is the example  of case guard.  In this example, I create a point object which include x, y coordinate. And a function called  quadrant. It uses to justify the quadrant of the point. Before, I write the case guard expression , the point class must be declared and implement deconstruct method so that its properties can be decompose in the pattern matching expression.

Wrap up 

In this article, I talk about the pattern matching which was introduced in C#7. It is a favourable way to improve the code style and enhance the code readability.  I has shown you three different kind of the pattern matching pattern usage by practical cases. And I introduce an advance pattern case guard in C#9. It can simply implement the multiple value compare at the same time by concise syntax.

I hope this material can help you to more understand the benefit of using pattern matching in C#. As always, thank for your reading. For better understanding , please check out the example from my github: https://github.com/Isaac234517/CSharpMatchingPattern

Get the latest article from lab.

Please leave your email below.

Subscribe

Thank you for you subscription