This scenario highlights the kinds of problems that pattern matching is well-suited to solve:
- The objects you need to work with aren't in an object hierarchy that matches your goals. You may be working with classes that are part of unrelated systems.
- The functionality you're adding isn't part of the core abstraction for these classes. The toll paid by a vehicle changes for different types of vehicles, but the toll isn't a core function of the vehicle.
When the shape of the data and the operations on that data are not described together, the pattern matching features in C# make it easier to work with.
The most basic toll calculation relies only on the vehicle type:
- A
Caris $2.00. - A
Taxiis $3.50. - A
Busis $5.00. - A
DeliveryTruckis $10.00
Create a new TollCalculator class, and implement pattern matching on the vehicle type to get the toll amount. The following code shows the initial implementation of the TollCalculator class's CalculateToll.
The preceding code uses a switch expression (not the same as a switch statement) that tests the type pattern. A switch expression begins with the variable, vehicle in the preceding code, followed by the switch keyword. Next comes all the switch arms inside curly braces. The switch expression makes other refinements to the syntax that surrounds the switch statement. The case keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The { } case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The { } case must follow the cases for each vehicle type. If the order were reversed, the { } case would take precedence. Finally, the null pattern detects when a null is passed to this method. The null pattern can be last because the other type patterns match only a non-null object of the correct type.
You're starting to see how patterns can help you create algorithms where the code and the data are separate. The switch expression tests the type and produces different values based on the results. That's only the beginning.