The article describes how pattern matching effectively utilizes and processes data in forms that weren’t part of the primary system.

Prerequisites:

Please review the articles below for a basic understanding of the C# 8.0 concepts required in this article.

New Switch syntax with C# 8.0

Intro to Property Pattern — C# 8.0

Let’s get started

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 the following conditions

Pattern matching program with new switch syntax

If the vehicle type matches with Car 100 is returned & so on. Notice that the null & {} are default cases for the object type.

Also, “_” can be used to program the default scenario. It’s a much more clean & efficient way of coding & it also recommends the use of 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 on vehicle type

Pattern Matching Switch

Refer to 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

Property Pattern Extended C#

GitHub Repo

ssukhpinder/PropertyPatternExample
Pattern matching in C# 8.0. Contribute to ssukhpinder/PropertyPatternExample development by creating an account on…github.com


Also published here.

If you liked this article, follow me on LinkedIn Instagram Facebook Twitter