Designing and coding

Menu

Tag Archives: Pattern Matching

Pattern Matching is a new feature which was introduced with C# 7.0 which allows us to write cleaner and concise code in many different scenarios. This feature can be said as an extension of is and as operators that we already have in C#. I wrote a post on it earlier, you can take a look here. This feature can be broadly devivded in two sections

Using Is Expression

Using Pattern Matching in Switch Statements

Using Is Expression:

Prior to C# 7.0, Is operator was used to check the type of a variable and based on the type, it returns true or false but with C# 7.0, Is Expression provides following three types of pattern matching

Const Pattern

Type Pattern

Var Pattern

Let’s discuss each with examples

Const Pattern: It allows us to check an object with any value. Let’s see an example

Type Pattern: It allows us to confirm the type of the object and also assigns the value to a new variable of the given type. Prior to C# 7.0, we also had the similar feature but here we can assign to the new variable as mentioned. Lets see the example

static void IsTypeExpression()
{
Object obj = 2;
var objPerson = new Person() { FirstName = "Brij", LastName="Mishra" };
var objNewPerson = new Employee() { FirstName = "Anvit", LastName = "Mishra", Company ="ABC Ltd" };
if (obj is int i)
Console.WriteLine($" Variable i has the value {i}");
if (objPerson is Person person)
Console.WriteLine($" p is of type {person.GetType().Name} and first name is {person.FirstName}");
if (objNewPerson is Employee newPerson)
Console.WriteLine($" p is of type {newPerson.GetType().Name} and first name is {newPerson.Company}");
if (objNewPerson is Person objPer)
Console.WriteLine($" p is of type {objPer.GetType().Name} and first name is {objPer.FirstName}");
}

Lets see the output

Here Employee inherits from Person. So in the last statement, we have the actual object of Employee but it assigned to a variable of base type.

Var Pattern: This is special case pattern where we check the type as var. This has one difference with the type pattern as it returns true even if it is null. Let’s see example

Here we can see that the second check is also true but since it is null, nothing it displayed where we wrote the type name. Now lets move to switch statements.

Pattern Matching in Switch Statements:

The patterns that we discussed in previous section, can be leveraged in switch statements as well. This becomes very handy when the number of testing conditions grows and using Is pattern becomes tough to maintain.

Earlier switch statement was only supporting constant pattern with limited value types and strings but now we can use Type and var patterns as well. Let’s see the example

Here Person is the base class and with the hierarchy Person->Employee->Manager. Based on the type of p case statement gets executed and in case of null, last one gets executed. Here, the key is the most derived type should be first statement. Compiler also gives a warning if the order is not correct. C# 6.0 also provides us capability to use When clause in switch case. Let’s see the example

Here we can see we can add additional filters in case statements which can be very useful in many scenarios. It also allows to add more that one filter and have various kind of combinations. I have used the var pattern as well in last case statement.

Hope you have enjoyed the post and will be able to use this feature in your day to day work.