Pattern Matching In C#


This article introduces new features of the Pattern Matching in C# 7.0. The C# 7 introduced pattern matching in two cases, the is expression and the switch statement. Pattern matching has following two advantages.

  1. Pattern matching can be performed on any data types. These can be user defined and primitive type.
  2. Pattern matching can extract values from the expression if the expression returns true.

These are enhanced by constant pattern, type pattern and var pattern.

You may like to read

  1. Ref Return and Ref Local In C#

is Expression with Pattern Matching


The is expression pattern matching uses is operator. When this expression is true then extracts value from that type and assigns to a variable. The is expression checks if an object is compatible with a given type. It is a combination of is and as operators. The is operator is used to check whether the run-time type of an object is compatible with a given type or not. The as operator is used to perform conversions between compatible types.

Let’s see an example. We define two classes Employee and SalariedEmployee. The following code snippet for Employee class.

The SalariedEmployee class is inherited by Employee class. The following code snippet for SalariedEmployee class.

Now create an entry program where we use these classes. We create an object for SalariedEmployee class. The following code snippet shows how to use is expression pattern matching.

As per above code, the is expression both tests the variable and assigns it to the new variable of proper type if its type is compatible with given type. In other words, variable emp assigns when the pattern matching expression has the true result. This is type pattern, As we determine this object is compatible with type or not. The variable which declares in the pattern has scope in if block only.

The C# 7 has one more pattern which is a constant pattern. In this pattern, the is expression pattern tests the variable with the constant value. Let’s see an example.

The var pattern is the third pattern. It always matches and simply puts the value of the input into a fresh variable with the same type. The variable declared with the var keyword is a real type of the object. Let see an example as per following code snippet.

switch Statement with Pattern Matching


When number of conditions grows the switch pattern matching expression becomes a better choice. The traditional switch statement was a constant pattern matching expression. We could compare a variable to any constant value used in a case statement. This constant was limited to numeric and string.

The switch statement has been enhanced in C# 7. Using case, We can check for a constant pattern, type pattern and var pattern. We’re generalizing the switch statement so that:

  1. We can switch on any type. It was limited to constant value.
  2. Patterns can be used in case clauses.
  3. Case clauses can have additional conditions on them.

Let’s see an example with switch statement using type pattern. We define an abstract class named Employee as per following code snippet.

Now define one more class named SalariedEmplyee as per following code snippet. This class is inherited by Employee class.

Now define one more class named WagedEmplyee as per following code snippet. This class is inherited by Employee class as well.

Now creates an entry program where we use these classes. We create an object for SalariedEmployee class. The following code snippet shows how to use switch expression pattern matching.

As per above code snippet, The case clause has type pattern. It matches with compatible type and executes accordingly. Here, first case block matches and show results.

We can use when clause in case expression. We can make special cases for those employees that have 0 WorkingDays by using a when clause on the case label. An Employee with 0 working days has a 0 income. We specify that condition using a when clause on the case label as per following code snippet.

As per above code snippet, There are some points:

  1. Multiple case labels can be applied to one switch section. The statement block is executed when one of those labels is true.
  2. When we check type with case expression then we don’t need to check for null additionally.
  3. As we can use case expression with when clause so there could be a chance to match multiple case labels. In this scenario, first one that matches get picked so the order of case clause is important.
  4. When we use default case and null clause then first null clause checks after that default clause check. No matter what sequence these have. Default clause is always evaluated last.

Conclusion


The pattern matching simplifies code and makes it more readable. It reduces code. Pattern matching as it is implemented in C# 7.0 is just beginning for this feature. The generic pattern matching is proposed in the C# 7.1.