Pattern Matching In C# 7.0

In this article, you will learn about pattern matching in C# 7.0.

Patterns are introduced in 7.0 to bring power to some existing language operators and statements. Currently, the pattern matching is partially implemented in C# 7.0 and more work is expected to be done in the next versions of the language. As described in the referenced document, the pattern matching feature introduced in C# 7.0 has the following two advantages:

  • You can perform pattern matching on any data type, even your own, whereas with if/else, you always need primitives to match.
  • Pattern matching can extract values from your expression.
  • C# 7.0 introduces pattern matching in two cases, the is expression and the switch statement.

Is expressions

The is expression checks if an object is compatible with a given type. The code listed in Listing 1 defines three classes, Person, Author, and Reader. Both Author and Reader classes are inherited from the Person class.

  1. // Person class      
  2. class Person {  
  3.     private string name;  
  4.     private Int16 age;  
  5.     private string sex;  
  6.     public string Name {  
  7.         get {  
  8.             return this.name;  
  9.         }  
  10.         set {  
  11.             this.name = value;  
  12.         }  
  13.     }  
  14.     public Int16 Age {  
  15.         get {  
  16.             return this.age;  
  17.         }  
  18.         set {  
  19.             this.age = value;  
  20.         }  
  21.     }  
  22.     public string Sex {  
  23.         get {  
  24.             return this.sex;  
  25.         }  
  26.         set {  
  27.             this.sex = value;  
  28.         }  
  29.     }  
  30. }  
  31. // Author class      
  32. class Author: Person {  
  33.     private string book_title;  
  34.     private Int16 pub_year;  
  35.     public string Title {  
  36.         get {  
  37.             return this.book_title;  
  38.         }  
  39.         set {  
  40.             this.book_title = value;  
  41.         }  
  42.     }  
  43.     public Int16 Year {  
  44.         get {  
  45.             return this.pub_year;  
  46.         }  
  47.         set {  
  48.             this.pub_year = value;  
  49.         }  
  50.     }  
  51.     public Author(string name, Int16 age, string sex, string book, Int16 year) {  
  52.         this.Name = name;  
  53.         this.Age = age;  
  54.         this.Sex = sex;  
  55.         this.book_title = book;  
  56.         this.pub_year = year;  
  57.     }  
  58. }  
  59. // Reader class      
  60. class Reader: Person {  
  61.     private string interest;  
  62.     private string job;  
  63.     public Reader(string name, Int16 age, string sex, string interest, string job) {  
  64.         this.Name = name;  
  65.         this.Age = age;  
  66.         this.Sex = sex;  
  67.         this.interest = interest;  
  68.         this.job = job;  
  69.     }  
  70. }    

Listing 1.

Now, let’s take a look at the code snippet in Listing 2, where we create three objects of types Person, Author, and Reader. Later, the code uses the is expression to see if these objects are compatible with the types or not. 

  1. Person p = new Person();  
  2. Author a = new Author("Mahesh Chand", 33, "male""ADO.NET Programming", 2003);  
  3. Reader r = new Reader("Ryan", 21, "male""reading""developer");  
  4. if (a is Person)  
  5. Console.WriteLine("Author is a Person");  
  6. else Console.WriteLine("Author is not a person");  
  7. if (a is Reader)  
  8. Console.WriteLine("Author is a Reader");  
  9. else Console.WriteLine("Author is not a Reader");  

 Listing 2.

Is expressions pattern

C# 7.0 extends the is expression by adding a pattern in the right side of the expression, that can be a value extracted from an expression. In C# 7.0, the following statement is correct. 

  1. if (a is Person p1)  

 Listing 3.

In Listing 3, the right side of the is can be a value returned by an expression.

Switch statement pattern

The Switch..case statement pattern enhances the case blocks by allowing us to compare the value of switch with the value returned by an expression, rather than a fixed value. The code snippet in Listing 4 compares an expression value.

  1. switch (r) {  
  2.     case Person person1 when(person1.Age == 33):  
  3.         Console.WriteLine($ "Person {person1.Name}");  
  4.         break;  
  5.     case Reader reader1 when(reader1.Age == 21):  
  6.         Console.WriteLine($ "Reader {reader1.Name}");  
  7.         break;  
  8.     default:  
  9.         Console.WriteLine("Not found");  
  10.         break;  
  11. }   

 Listing 4.

In the above code snippet in Listing 4, r is both Person and Reader, but the Age is 21. Thus, the second case is matched.

Summary

C# 7.0 introduced a new feature, pattern matching. In this article, we learned about two patterns and how to use them in our code.

Next C# 7.0 Feature >>  Deconstruction in C# 7.0

References

References used to write this article -

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/