Pattern matching with objects
In Chapter 3, Controlling Flow, Converting Types, and Handling Exceptions, you were introduced to basic pattern matching. In this section, we will explore pattern matching in more detail.
Defining flight passengers
In this example, we will define some classes that represent various types of passengers on a flight and then we will use a switch expression with pattern matching to determine the cost of their flight:
- In the
PacktLibraryNetStandard2
project/folder, add a new file namedFlightPatterns.cs
. - In
FlightPatterns.cs
, add statements to define three types of passenger with different properties, as shown in the following code:namespace Packt.Shared; public class Passenger { public string? Name { get; set; } } public class BusinessClassPassenger : Passenger { public override string ToString() { return $"Business Class: {Name}"; } } public class FirstClassPassenger : Passenger { public int AirMiles...