C# CONCEPTS
The article describes how pattern matching provides an effective way to utilize and process that data in forms that weren’t part of the primary system.
Prerequisites
Please go through the articles below for a basic understanding of C# 8.0 concepts required in this article.
Intro to Property Pattern — C# 8.0
Lets Start
Let’s take an example of Toll Calculator and see how pattern matching helps to write an algorithm for that.
Entity class used throughout the article
Example 1: Calculate toll fare as per following conditions:
If the vehicle is Car => 100 Rs
If the vehicle is DeliveryTruck => 200 Rs
If the vehicle is Bus => 150 Rs
If the vehicle is Taxi => 120 Rs
Pattern matching program with new switch syntax
If the vehicle type matches with Car 100 is returned & so on. Notice the null & {} are default cases for object type.
Also, “_” can be used to program the default scenario. Refer new switch syntax.
Its a much more cleaned & efficient way of coding & also recommended use single-letter variable names inside the switch syntax.
public static int TollFare(Object vehicleType) => vehicleType switch
{
Car c => 100,
DeliveryTruck d => 200,
Bus b => 150,
Taxi t => 120,
null => 0,
{ } => 0
};
Test above program
Test examples from a console application standpoint. The below code illustrates how to call the above pattern-matching function from the main method.
var car = new Car();
var taxi = new Taxi();
var bus = new Bus();
var truck = new DeliveryTruck();
Console.WriteLine($"The toll for a car is {TollFare(car)}");
Console.WriteLine($"The toll for a taxi is {TollFare(taxi)}");
Console.WriteLine($"The toll for a bus is {TollFare(bus)}");
Console.WriteLine($"The toll for a truck is {TollFare(truck)}");
Console Output
The toll for a car is 100
The toll for a taxi is 120
The toll for a bus is 150
The toll for a truck is 200
Example 2: Add occupancy pricing based upon vehicle type
Cars & taxis with “NO” passengers pay an extra 10 Rs.
Cars & taxis with two passengers get a 10 Rs discount.
Cars & taxis with three or more passengers get a 20 Rs discount.
Buses that are less than 50% of passengers pay an extra 30 Rs.
Buses that are more than 90% of passengers get a 40 Rs discount.
Trucks over 5000 lbs, charged an extra 100 Rs.
Light trucks under 3000 lbs, given a 20 Rs discount.
Pattern Matching Switch
Refer pattern matching syntax with single & multiple property classes. Link
Pattern Matching — Car Entity
Car { PassengerCount: 0 } => 100 + 10,
Car { PassengerCount: 1 } => 100,
Car { PassengerCount: 2 } => 100 - 10,
Car c => 100 - 20,
Pattern Matching — Taxi Entity
Taxi {Fare:0 }=>100+10,
Taxi { Fare: 1 } => 100,
Taxi { Fare: 2 } => 100 - 10,
Taxi t => 100 - 20,
Pattern Matching — Bus Entity
Bus b when ((double)b.RidersCount / (double)b.Capacity) < 0.50 => 150 + 30,
Bus b when ((double)b.RidersCount / (double)b.Capacity) > 0.90 => 150 - 40,
Bus b => 150,
Pattern Matching — Delivery Truck Entity
DeliveryTruck t when (t.Weight > 5000) => 200 + 100,
DeliveryTruck t when (t.Weight < 3000) => 200 - 20,
DeliveryTruck t => 200,
Combining all entities
The below example highlights the advantages of pattern matching: the pattern branches are compiled in order. The compiler also warns about the unreachable code.
Test above program
Test examples from a console application standpoint. The below code illustrates how to call the above pattern-matching function from the main method.
var car1 = new Car{ PassengerCount=2};
var taxi1 = new Taxi { Fare = 0 };
var bus1 = new Bus { Capacity = 100, RidersCount = 30 };
var truck1 = new DeliveryTruck { Weight = 30000 };
Console.WriteLine($"The toll for a car is {OccupancyTypeTollFare(car1)}");
Console.WriteLine($"The toll for a taxi is {OccupancyTypeTollFare(taxi1)}");
Console.WriteLine($"The toll for a bus is {OccupancyTypeTollFare(bus1)}");
Console.WriteLine($"The toll for a truck is {OccupancyTypeTollFare(truck1)}");
Console Output
The toll for a car is 90
The toll for a taxi is 110
The toll for a bus is 180
The toll for a truck is 300
“Pattern matching makes code more readable and offers an alternative to object-oriented techniques when you can’t add code to your classes.”
Extended Property Pattern
GitHub Repo
ssukhpinder/PropertyPatternExample
More C# 8.0 Concepts
Null-Coalescing assignment C# 8.0
Configuration Providers in .NET
Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.