paint-brush
Advanced Property Pattern Techniques in C# 8.0 by@ssukhpinder
383 reads
383 reads

Advanced Property Pattern Techniques in C# 8.0

by Sukhpinder SinghMarch 5th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The article describes how pattern matching effectively utilises and processes data in forms that weren’t part of the primary system. Pattern matching can be used to write an algorithm for toll calculators. The code below illustrates how to call the above pattern-matching function from the main method.
featured image - Advanced Property Pattern Techniques in C# 8.0
Sukhpinder Singh HackerNoon profile picture

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


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

  • 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 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

  • 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 with over 90% of passengers get a 40 Rs discount.
  • Trucks over 5000 lbs are charged an extra 100 Rs.
  • Light trucks under 3000 lbs, given a 20 Rs discount.

Pattern Matching Switch

Refer to pattern-matching syntax with single & multiple property classes.

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


Also published


If you liked this article, follow me on
바카라사이트 바카라사이트 온라인바카라