Pattern Matching In C# 9.0

Introduction

 
In today’s article we will look at Pattern Matching in C# 9.0. C# 9.0 has been introduced with .NET 5.0. We will look at what improvements to Pattern Matching have been introduced with C# 9.0.

Using C# 9.0

 
Microsoft recently announced the availability of .NET 5 release candidate at Microsoft Ignite 2020. This included the latest features in C# 9.0. In order to code in .NET 5.0, we would need to install the latest preview of Visual Studio 2019 (Version 16.8.0 Preview 3.1). As I had read about some cool features in C# 9.0including the improvements to pattern matching, I downloaded and installed the required version as below,
 
Pattern Matching in C# 9.0
 
In today’s applications, the application logic is more and more dependent on the data we have in the classes rather than just the simple data types in it. Hence, better methods of pattern matching are required to keep our code clean and simple. Let us look at some examples of doing this.
 

Using Pattern Matching

 
Let us create a console application in Visual Studio 2019 (Version 16.8.0 Preview 3.1) as below,
 
Pattern Matching in C# 9.0
 
Pattern Matching in C# 9.0
 
Pattern Matching in C# 9.0
 
Next, we will change the properties of the project and set the Target Framework to .NET 5.0, as below,
 
Pattern Matching in C# 9.0
 
Now, add the below code,
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace ConsoleAppPatternMatching {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             var emp1 = new Employee() {  
  7.                 Id = 1,  
  8.                     Name = "John Doe",  
  9.                     Salary = 2000  
  10.             };  
  11.             var emp2 = new Employee() {  
  12.                 Id = 2,  
  13.                     Name = "Jane Doe",  
  14.                     Salary = 3000  
  15.             };  
  16.             var employees = new List < Employee > ();  
  17.             employees.Add(emp1);  
  18.             employees.Add(emp2);  
  19.             foreach(var employee in employees) {  
  20.                 Console.WriteLine($ "Employee Name {employee.Name} has Salary {employee.Salary} and Tax {CalculateTax(employee)}");  
  21.             }  
  22.             foreach(var employee in employees) {  
  23.                 if (employee is {  
  24.                         Id: 1  
  25.                     }) Console.WriteLine($ "Employee Name {employee.Name} has Salary {employee.Salary}");  
  26.             }  
  27.             Console.ReadKey();  
  28.         }  
  29.         static int CalculateTax(Employee employee) => employee.Salary  
  30.         switch {  
  31.             >= 3000 => 500, < 3000 => 250  
  32.         };  
  33.     }  
  34.     public class Employee {  
  35.         public int Id {  
  36.             get;  
  37.             set;  
  38.         }  
  39.         public string Name {  
  40.             get;  
  41.             set;  
  42.         }  
  43.         public int Salary {  
  44.             get;  
  45.             set;  
  46.         }  
  47.     }  
  48. }  
In the above code (first for-each loop), you can see that we pass the instance of the employee class to a function, which based on the Salary amount returns the applicable tax. This could also be done by writing an IF statement and looking through the property. However, this is a short method of achieving our goal and it keeps our code clean and simple.
 
In the second for-each loop, we are simply checking the value of a field in an IF statement and based on a certain value we execute the IF statement. This again keeps our code clean and simple.
 
When we run the above code, we get the following output,
 
Pattern Matching in C# 9.0
 

Summary

 
In this article, we looked at the Pattern Matching improvements that have been introduced with C# 9.0. As we saw, most of the work can be done using the old method of checking property values and acting on them. Hence, we can say that these new features add synthetic sugar to our code. However, it is this syntactic sugar that makes our code sweet and reduces the lines of code. Happy Coding!