Pattern Multiple inputs C#


You can write patterns that examine multiple properties of an object.

public record Order(int Items, decimal Cost);

 

The following code examines the number of items and the value of an order to calculate a discounted price:

public decimal CalculateDiscount(Order order) =>
    order switch
    {
        { Items: > 10, Cost: > 1000.00m } => 0.10m,
        { Items: > 5, Cost: > 500.00m } => 0.05m,
        { Cost: > 250.00m } => 0.02m,
        null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"),
        var someObject => 0m,
    };

 

Source: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching