In this article, we are going to cover a new addition to C# 10, known as extended property patterns. Pattern matching appears to be one of the priorities in the development of C# as a language in the last several years.

Every new release adds a few improvements to this paradigm, so let’s have a look at what’s new in this version.

To download the source code for this article, you can visit our GitHub repository.

What Is Pattern Matching?

Pattern matching is a paradigm for evaluating expressions to check whether they meet certain criteria. In other words, it is an evolution of the good old if-else instructions, which, in some cases, allows for a more concise and clear code.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Pattern matching is ubiquitous in functional languages, such as F#, whereas C# started supporting it since version 7.

Property Patterns Matching

Property patterns added in C# 8.0 allow matching based on the properties of the object.

Let’s see how we can use them in a switch expression to evaluate a set of conditions.

First, we are going to declare a simplified data model, but with enough properties to show the sweetness of the new syntactic sugar:

record Order(Payment Payment, Customer Customer);
record Payment(Price Price, DateTime? PaymentDate = null);
record Price(string Currency, decimal Amount);
record Customer(string Name, string Group);

Now, we use this Order to create an instance of an OrderProcessor class in a variation of a factory pattern:

public static string GetPreCSharp10Syntax(Order order) => order switch
{
    { Payment: {  Price: { Currency: "BTC" } } } => "CryptoOrderProcessor",
    { Customer: { Group: "Banking" }, Payment: { Price: { Currency: "JPY" } } } => "JapaneseBankingProcessor",
    { Payment: { Price: { Amount: > 5000000} } } or { Customer: { Group: "VIP" } } => "ImportantOrderProcessor",
    _ => "DefaultOrderProcessor"
};

The example shows the switch expression combined with property pattern matching.

The usage of pattern matching and expression-bodied members makes the syntax very succinct and reduces the overall size of the method. Although the syntax isn’t terrible, the way of accessing the nested properties feels a bit unnatural.

Extended Property Patterns Matching

The extended property patterns build on top of the existing feature. They allow a more concise and readable syntax, by offering the well-known dot-notation for accessing properties of objects, rather than requiring usage of curly braces.

Let’s see the same example rewritten with the new approach:

static class OrderProcessorFactory
{
    public static string Get(Order order) => order switch
    {
        { Payment.Price.Currency: "BTC" } => "CryptoOrderProcessor",
        { Customer.Group: "Banking", Payment.Price.Currency: "JPY" } => "JapaneseBankingProcessor",
        { Payment.Price.Amount: >= 500000 } or { Customer.Group: "VIP" } => "ImportantOrderProcessor",
        _ => "DefaultOrderProcessor"
    };
}

Clearly, this improvement is not a revolution, but a very welcome evolution.

Conclusion

In this article, we had a glance at the property pattern matching improvement added in C# 10. While it’s not a ground-breaking change, it surely allows a more “natural” and fluent syntax, which will positively affect the pattern matching adoption in C#.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!