Exploring C# 10: Use Extended Property Patterns to Easily Access Nested Properties
Tags: csharp, dotnet
Published: 2021-11-18 16:34:45

---
title: Exploring C# 10: Use Extended Property Patterns to Easily Access Nested Properties
published: true
date: 2021-11-18 01:34:28 UTC
tags: csharp, dotnet
canonical_url: https://www.daveabrock.com/2021/11/18/exploring-c-10-use-extended-property-patterns-to-easily-access-nested-properties/
---

Welcome back to my series on new C# 10 features. So far we've talked about [file-scoped namespaces](https://www.daveabrock.com/2021/10/05/csharp-10-file-scoped-namespaces/) and [global using declarations](https://www.daveabrock.com/2021/10/21/csharp-10-global-usings/). Today, we'll talk about extended property patterns in C# 10.

Over the last few years, C# has made a lot of property pattern enhancements. Starting with C# 8, the language has supported [extended property patterns](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#property-patterns), a way to match an object's specific properties.

For our example, let's continue the superhero theme with a `Person`, a `Superhero` that inherits from `Person`, and a `Suit`. We're going to calculate any special surcharges when making a particular superhero's suit.  

```csharp
public class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string? Address { get; set; }
    public string? City { get; set; }
}

public class Superhero : Person
{
    public int MaxSpeed { get; set; }
    public Suit? Suit { get; set; }
}

public class Suit
{
    public string? Color { get; set; }
    public bool HasCape { get; set; }
    public string WordsToPrint { get; set; }
}
```

Now let's create a quick object for Iron Man:

```csharp
var tonyStark = new Superhero()
{
    FirstName = "Tony",
    LastName = "Stark",
    Address = "10880 Malibu Point",
    City = "Malibu",
    Suits = new List<Suit>
    {
        new Suit
        {
            Color = "Red",
            HasCape = false
        }
    }
};
```

In our switch expression, let's say we want to determine special pricing based on a particular suit's color.

Here's how we'd do it in C# 9:

```csharp
public static int CalculateSuitSurcharge(Superhero hero) =>

        hero switch
        {
            { Suit: { Color: "Blue" } } => 100,
            { Suit: { Color: "Yellow" } } => 200,
            { Suit: { Color: "Red" } } => 300
            _ => 0
        };
```

With C# 10, we can use dot notation to make it a bit cleaner:

```csharp
public static int CalculateSuitSurcharge(Superhero hero) =>

        hero switch
        {
            { Suit.Color: "Blue" } => 100,
            { Suit.Color: "Yellow" } => 200,
            { Suit.Color: "Red" } => 300
            _ => 0
        };
```

Much like before, you can also use multiple expressions at once. Let's say we wanted to charge a surcharge for a notoriously difficult customer. Look at how we can combine multiple properties in a single line.

```csharp
public static int CalculateSuitSurcharge(Superhero hero) =>

        hero switch
        {
            { Suit.Color: "Blue" } => 100,
            { Suit.Color: "Yellow" } => 200,
            { Suit.Color: "Red", Address: "10880 Malibu Point" } => 500,
            { Suit.Color: "Red" } => 300
            _ => 0
        };
```

That's all there is to it. Will this feature change your life? No. Even so, it's a nice update that allows for cleaner and simpler code.

There's a lot more you can do with property patterns as well, so I'd recommend hitting up the [Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#property-pattern) to learn more.

If you're interested in the design proposal, you can also find it [here](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/extended-property-patterns).

Let me know your thoughts below. Happy coding!