July 2017
Beginner
466 pages
9h 37m
English
In this type of pattern matching, it introduces a new pattern variable out of the expression, allowing you to extract the value of the type. It is similar to the out variable, but with a limited scope to the surroundings.
Let's look at an example. In the old method, we need to check for the data type, and then we must convert the variable to the specified data type to get the value out of it. Here, if the person is of type Employee, we can type cast it to Employee to get the values of the employee object:
if (person is Employee) { Employee employee = (Employee)person; Console.WriteLine("Person '{0}' is an Employee", employee.Firstname); } else if (person is Customer) { Customer customer = (Customer)person; ...Read now
Unlock full access