As we are aware of is-Expression where we can use is keyword to verify the given type. In C# 7.0, this has been extended to constant, type as well as var patterns. Let’s dig into each of the patterns here. 

1) Pattern Matching with constants

Earlier in is-Expressions, we use to match for variable types, now from C# 7.0 we can even verify and validate for constants as below.

public void PatternMatchingWithConstants() 
 { 
     int age = 50; 

     if(age is 50) 
     { 
         //Do write some logic here 
     } 
 }

As observed in the above code, we are validating a constant value using is-Expression in if block.

2) Pattern Matching with Types

As we see in the above, we validated the constants and similarly, we can validate the type of the given variable in the if block as shown below.

public void PatternMatchingWithType() 
{ 
    object age = 50; 
 
    if (age is int) 
    { 
         int myage = (int)age; 
        //Do write some logic here 
    } 
}

Addition to this, we were given with other flexibility here. As observed in the above code, we perform two actions one is to verify the age type is int or not and once it is true then we are type casting it to another variable. Now, we can do these two actions in single step I.e.., validating and assigning to a variable if condition success.

object age = 50; 
 
if (age is int myage) 
{ 
    //Do write some logic here 
}

In the above code age will be verified as type int or not and once it is verified as int type then it will be type casted and assign to myage in a single step.

Type pattern matching can also be applied at inheritance. It will verify whether the given class object is inherited by a base class or not as shown below.

public class Animal 
{ 
    public string Name { get; set; } 
} 
 public class Dog: Animal 
{ 
} 
public class Cat : Animal 
{ 
} 
  
public class Main 
{ 
    public void GetAnimal() 
    { 
        Dog dog = new Dog(); 
  
        if(dog is Animal animal) 
        { 
            //Do write some logic here 
        } 
    } 
}

The above code is self-explanatory, here we can even verify the given object is of type base class or not.

Few points need to note about this pattern is, the match will fail when an object set to null and even the type matches.

public void GetAnimal() 
{ 
    Dog dog = null; 
  
    if (dog is Dog) //returns false 
    { 
        //Do write some logic here 
    } 
}

Another point to note is, we can use this feature in any logical expressions too as shown below. 

public void GetAnimal() 
{ 
    Dog dog = new Dog(); 
  
    if(dog is Animal animal && animal.Name == "Dog") 
    { 
        //Do write some logic here 
    } 
}

 3) Pattern Matching with Var

Variable Pattern is used as similar as constants and type but here the is-Expression will always return true and typecast to the given type. 

public void GetAnimalWithVar() 
{ 
    Dog dog = new Dog() { Name="Dog" }; 
    Cat cat = new Cat() { Name = "Cat"}; 
  
    PrintAnimalName(dog); 
    PrintAnimalName(cat); 
  
    //Local functions in C# 7.0 
    void PrintAnimalName(Animal obj) 
    { 
        if (obj is var animal) //Always returns true 
        { 
            Console.WriteLine(animal.Name); 
        } 
     
    } 
}

As observed every time it calls PrintAnimalName() method, it will return respectively given names to each provided object.

Hope, this explanation gives you the concept.

You can see complete code here in GitHub

Happy Coding 🙂