C# CONCEPTS

Advance Property Pattern C# 8.0

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.

Sukhpinder Singh
Aug 26, 2020 · 4 min read
Image for post
Image for post

Prerequisites

Lets Start

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

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

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

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

Test above program

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

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Sukhpinder Singh

Written by

Programmer by heart | C# | Python | .Net Core | Xamarin | Angular | AWS | Remember to 👏👏👏👏 | Linkedin 👉 https://www.linkedin.com/in/sukhpinder-singh/

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Sukhpinder Singh

Written by

Programmer by heart | C# | Python | .Net Core | Xamarin | Angular | AWS | Remember to 👏👏👏👏 | Linkedin 👉 https://www.linkedin.com/in/sukhpinder-singh/

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store