CSharp 10 Features

Last Updated: 4/18/2022

Extended Property Patterns

You use a property pattern to match an expression's properties or fields against nested patterns

Property Pattern Syntax

{ Prop1: { Prop2: pattern } }

Extended Property Pattern Syntax

{ Prop1.Prop2: pattern } 
	
public class Address
{
	public string City {get; set;}
}

public class Person
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
	public Address Address { get; set; }
}

object obj = new Person { 
	FirstName = "Vijay", 
	LastName = "Kumar", 
	Address = new Address {
		City = "Chennai"
	}
};

//Property Pattern
if(obj is Person { Address: {City: "Chennai" } })
	Console.WriteLine("Chennai");

//Extended Property Pattern
if(obj is Person { Address.City: "Chennai" })
	Console.WriteLine("Chennai");

Example