Pattern Matching In C#7.0

Introduction

 
C# pattern matching is a feature that allows us to perform matching on data or any object. We can perform pattern matching using the Is expression and Switch statement. Is expression is used to check, whether an object is compatible with the given type or not? A switch statement has been enhanced with the const pattern, the type pattern, and the var pattern.
 

“is” Operator with Pattern Matching

 
The “is” operator is available since the first version of C#. This operator can be used to check if an object is compatible with a specific type. With the C# 7.0 extensions, the “is” operator can be used to check for patterns.
 
Const Pattern
 
One available pattern in C# 7.0 is the const pattern. The below example will explain the const pattern.
  1. //Constant Patterns  
  2. if (o is null) Console.WriteLine($ "Object is null");  
  3. if (o.ValConstant is 32) Console.WriteLine($” o.ValConstant is 32 ");  
Type Pattern
 
With this pattern, you can verify if the object is compatible with the specific type.
  1. //Type Patterns  
  2. if (o.ValConstant is int i) Console.WriteLine($ "its a type pattern with an int and its value = {i}");  
  3. if (o is Associate A) Console.WriteLine($ "its a type pattern with Associate and FirstName ={A.FirstName}");  
Var Pattern
 
This type is using the var keyword. Here the object is always type.
  1. //Var Pattern  
  2. if (o is  
  3.     var aa) Console.WriteLine($ "its a var type type : {aa?.GetType()?.Name}");  
  4. namespace PatternMatching {  
  5.     class Program {  
  6.         static void Main(string[] args) {  
  7.             var records = new List < Associate > {  
  8.                 new Associate("Prasad", 32, null),  
  9.                 new Associate("Praveen", 58, null)  
  10.             };  
  11.             foreach(var obj in records) {  
  12.                 IsPatternMatching(obj);  
  13.             }  
  14.         }  
  15.         private static void IsPatternMatching(Associate o) {  
  16.             //Constant Patterns  
  17.             if (o is null) Console.WriteLine($ "Object is null");  
  18.             if (o.ValConstant is 32) Console.WriteLine($ "{o.ValConstant} is constant");  
  19.             //Type Patterns  
  20.             if (o.ValConstant is int i) Console.WriteLine($ "its a type pattern with an int and its value = {i}");  
  21.             if (o is Associate A) Console.WriteLine($ "its a type pattern with Associate and FirstName ={A.FirstName}");  
  22.             //Var Pattern  
  23.             if (o is  
  24.                 var aa) Console.WriteLine($ "its a var type type : {aa?.GetType()?.Name}");  
  25.         }  
  26.     }  
  27.     public class Associate {  
  28.         public string FirstName {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         public int ValConstant {  
  33.             get;  
  34.             set;  
  35.         }  
  36.         public string LastName {  
  37.             get;  
  38.             set;  
  39.         }  
  40.         public Associate(string firstName, int value, string lastName) {  
  41.             FirstName = firstName;  
  42.             ValConstant = value;  
  43.             LastName = lastName;  
  44.         }  
  45.     }  
  46. }  

“switch” Operator with Pattern Matching

 
The switch statement has been enhanced with const, type, and var pattern.
 
Const Pattern
 
Using case statement, const pattern can be implemented.
  1. /// <summary>  
  2. /// Constant Pattern Matching  
  3. /// </summary>  
  4. private static void ConstantPatternMatching() {  
  5.     object obj = 34;  
  6.     switch (obj) {  
  7.         case 34:  
  8.             Console.WriteLine($ "obj is constant");  
  9.             break;  
  10.     }  
  11. }  
Type Pattern
 
It checks if an object is of the specified type
  1. /// <summary>  
  2. /// Type PatternMatching  
  3. /// </summary>  
  4. private static void TypePatternMatching() {  
  5.     Object obj = Console.Error;  
  6.     switch (obj) {  
  7.         case TextWriter textwriter:  
  8.             textwriter.WriteLine($ "Object is TextWriter");  
  9.             break;  
  10.     }  
  11. }  
Var Pattern and Conditional clause
 
It always matches and puts the value into a new variable
  1. /// <summary>  
  2. /// Var pattern matching Conditional clause  
  3. /// </summary>  
  4. private static void VarPatternMatching() {  
  5.     int obj = 100;  
  6.     switch (obj) {  
  7.         case var obj1 when obj1 > 50: Console.WriteLine($ "obj1 is a variant of value {obj1}");  
  8.         break;  
  9.     }  
  10. }  
Full source code given below:
  1. namespace PatternMatching {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             ConstantPatternMatching();  
  5.             TypePatternMatching();  
  6.             VarPatternMatching();  
  7.         }  
  8.         /// <summary>  
  9.         /// Constant Pattern Matching  
  10.         /// </summary>  
  11.         private static void ConstantPatternMatching() {  
  12.             object obj = 34;  
  13.             switch (obj) {  
  14.                 case 34:  
  15.                     Console.WriteLine($ "obj is constant");  
  16.                     break;  
  17.             }  
  18.         }  
  19.         /// <summary>  
  20.         /// Type PatternMatching  
  21.         /// </summary>  
  22.         private static void TypePatternMatching() {  
  23.             Object obj = Console.Error;  
  24.             switch (obj) {  
  25.                 case TextWriter textwriter:  
  26.                     textwriter.WriteLine($ "Object is TextWriter");  
  27.                     break;  
  28.             }  
  29.         }  
  30.         /// <summary>  
  31.         /// Var pattern matching Conditional clause  
  32.         /// </summary>  
  33.         private static void VarPatternMatching() {  
  34.             int obj = 100;  
  35.             switch (obj) {  
  36.                 case var obj1 when obj1 > 50: Console.WriteLine($ "obj1 is a variant of value {obj1}");  
  37.                 break;  
  38.             }  
  39.         }  
  40.     }  
  41. }  

Summary

 
In C# 7.0, pattern matching gives us yet another feature that can simplify and reduce your code. Furthermore, “is” and “switch/case” have been enhanced to support const, type, and var patterns.
Next Recommended Reading Expression Bodies In C# 7.0