14

C# 10.0: Extended Property Patterns – Use the Dot Token to Access Nested Members

 2 years ago
source link: https://www.thomasclaudiushuber.com/2021/10/21/c-10-extended-property-patterns/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C# 10.0: Extended Property Patterns – Use the Dot Token to Access Nested Members

In the previous blog posts you learned about different C# 10.0 features:

In this blog post, let’s look at another interesting feature of C# 10.0 which is called extended property patterns.

Property Patterns in C# 9.0

I’ve written a bigger blog post about pattern matching in C# 9.0. If you’re not familiar with pattern matching, I recommend you to read that blog post.

In that blog post, we created a class hierarchy like you see it below. As you can see, there is a Person class, and the two classes Developer and Manager that inherit from Person. Note also that the Developer class has a Manager property of type Manager.

public class Person
{
  public string? FirstName { get; set; }
  public int YearOfBirth { get; set; }
}
public class Developer : Person
{
  public Manager? Manager { get; set; }
}
public class Manager : Person
{
}

If you want to check if a variable contains a Developer instance that has a Manager with the firstname Thomas, you can do that in C# 9.0 as below:

if (obj is Developer { Manager: { FirstName: "Thomas" } } developerWithThomasAsManager)
{
  // Use the developerWithThomasAsManager variable here
}

Extended Property Patterns in C# 10.0

In C# 10.0, you can use the . token to access members in a property pattern. This is called extended property patterns. The code snippet below uses the . token to check if the Manager‘s FirstName property contains the value Thomas. This means that the code snippet below does exactly the same as the code snippet above.

if (obj is Developer { Manager.FirstName: "Thomas" } developerWithThomasAsManager)
{
  // Use the developerWithThomasAsManager variable here
}

Important to mention is that if the Manager property of the Developer instance is null, you do not get a NullReferenceException. You could think that you get one, because you wrote Manager.FirstName. But remember, you’re writing a pattern here to check the shape of the object, you’re not directly accessing that property with your code, so there won’t be a NullReferenceException. That means if the Manager property is null, the pattern { Manager.FirstName: "Thomas" } does not match, and the condition of the if statement above evaluates to false, and the if block is not entered. That’s it.

The More Nesting You Have, the Greater This Feature Is

Let’s say you want to check if the Manager‘s firstname has a length of 6. In C# 9.0 you do this by nesting property patterns to access the Length property of the string type, which is the type of the FirstName property. You see this in the code snippet below:

if (obj is Developer { Manager: { FirstName: { Length: 6 } } } developerWithManagerFirstnameLengthOfSix)
{
  // Use the developerWithManagerFirstnameLengthOfSix variable here
}

In the next code snippet you see the C# 10.0 way to check if a developer’s manager has a firstname with a length of 6. The extended property patterns with the . token make this super easy to read:

if (obj is Developer { Manager.FirstName.Length: 6 } developerWithManagerFirstnameLengthOfSix)
{
  // Use the developerWithManagerFirstnameLengthOfSix variable here
}

Use Multiple Extended Property Patterns

You can also use multiple extended property patterns. What if you want to check for example if a developer has a manager who has a firstname with a length of 6 and who (=the manager) is born in 1980? Below you see the C# 9.0 way to do this:

if (obj is Developer { Manager: { FirstName: { Length: 6 }, YearOfBirth: 1980 } } dev)
{
  // Use the dev variable here
}

In C# 10.0, you can write the same code as below with an extended property pattern to access the Length property of the manager’s firstname:

if (obj is Developer { Manager: { FirstName.Length: 6, YearOfBirth: 1980 } } dev)
{
    // Use the dev variable here
}

You can even go further in C# 10.0 and use the . token also to access the Manager‘s FirstName and YearOfBirth properties like you see it in the code snippet below:

if (obj is Developer { Manager.FirstName.Length: 6, Manager.YearOfBirth: 1980 } dev)
{
  // Use the dev variable here
}

Personally, I am a fan of the very last code snippet here, because when I read it, I recognize very fast that the pattern checks the properties FirstName and YearOfBirth of the Manager, and not those of the Developer. So, using the . token can increase readability.

Summary

As you saw in this blog post, extended property patterns are a little C# 10.0 language extension that let’s you use the . token to access nested properties when you use a property pattern. As mentioned at the beginning of this blog post, if you’re not familiar with pattern matching in C#, my blog post about pattern matching in C# 9.0 is a good read.

In the next blog post, you will learn about Record structs in C# 10.0.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Spam protection: Sum of 1 + 5 ? *

Notify me of follow-up comments by email.

Notify me of new posts by email.

This site uses Akismet to reduce spam. Learn how your comment data is processed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK