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, and var patterns as well. Let's dig into each of the patterns here.
Pattern Matching with constants
Earlier in is-Expressions, we used to match the variable types but now, we can even verify and validate for constants too. See the code below.
- public void PatternMatchingWithConstants() {
- int age = 50;
- if (age is 50) {
-
- }
- }
As observed in the above code, we are validating a constant value using is-Expression in an "if" block.
Pattern Matching with Types
As we see in the above code, we validated the constants. Similarly, we can validate the type of a given variable in the "if" block as shown below.
- public void PatternMatchingWithType() {
- object age = 50;
- if (age is int) {
- int myage = (int) age;
-
- }
- }
In addition to this, we are given other flexibility here. As observed in the above code, we perform two actions. First, we are verifying if 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 a single step, i.e., validating and assigning to a variable the if condition success.
- object age = 50;
- if (age is int myage) {
-
- }
In the above code, the age will be verified as type int or not. Then, it will be type-casted and assigned 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) {
-
- }
- }
- }
The above code is self-explanatory. Here, we can even verify if the given object is of type base class or not.
The point to be noted here is that the match will fail when an object is set to null, even if the type matches.
- public void GetAnimal() {
- Dog dog = null;
- if (dog is Dog)
- {
-
- }
- }
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") {
-
- }
- }
Pattern Matching with Var
Variable Pattern is used similar to the 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);
-
- void PrintAnimalName(Animal obj) {
- if (obj is
- var animal)
- {
- Console.WriteLine(animal.Name);
- }
- }
- }
As observed, every time it calls the PrintAnimalName() method, it will return the respective given names to each provided object.
Hope, this explanation gives you the concept.
You can see the complete code here on GitHub.