C# > Functional Programming > Pattern Matching > Property Patterns
Property Patterns in C#
This snippet demonstrates property patterns in C#, a powerful feature introduced in C# 8.0. Property patterns allow you to match on specific properties of an object, making your code more readable and concise.
Basic Property Pattern Example
This example defines two classes, Address
and Person
. The GetLocation
method uses a property pattern to match on the Address
property of the Person
object. The first pattern checks if the person lives in London, England. The second checks if they live in California. The third pattern checks for a specific first name. If none of the patterns match, the last pattern ({ }
) acts as a default case.
public class Address
{
public string City { get; set; }
public string State { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public static class PropertyPatternExample
{
public static string GetLocation(Person person)
{
return person switch
{
{ Address: { City: "London", State: "England" } } => "Lives in London, England",
{ Address: { State: "California" } } => "Lives in California",
{ FirstName: "John" } => "First name is John",
{ } => "Location unknown"
};
}
}
Concepts Behind Property Patterns
Property patterns allow you to inspect the properties of an object and make decisions based on their values. This makes your code more expressive and easier to understand. They are a form of pattern matching introduced in C# 8.0 that focuses on the properties of an object. Unlike traditional conditional statements, property patterns provide a more declarative and concise way to express complex conditions based on object properties.
Real-Life Use Case
Consider a scenario where you are building a user interface and need to display different information based on the user's role and location. You can use property patterns to easily determine which information to display. For example:
if (user is { Role: "Admin", Location: "USA" }) { // Display admin panel for US users } else if (user is { Role: "Editor", Location: "Europe" }) { // Display editor panel for European users } else { // Display default user interface }
Property patterns make this kind of logic much cleaner and more readable than nested if
statements.
Best Practices
Interview Tip
When discussing property patterns in an interview, be prepared to explain their benefits over traditional conditional statements (readability, conciseness, and expressiveness). Also, be ready to discuss potential drawbacks, such as performance implications with overly complex patterns. Mention that Property Patterns can be combined with positional and var patterns for even richer functionality.
When to Use Them
Use property patterns when you need to check multiple properties of an object in a single conditional statement. They are particularly useful when dealing with complex object structures and nested properties. When you want to avoid long chains of `if-else` statements, and write code in a more declarative, functional style. When you are dealing with immutable objects (like records) where modifying properties is not an option, pattern matching allows you to react to different object states elegantly.
Memory Footprint
Property patterns themselves don't have a significant memory footprint. The memory usage is primarily determined by the objects being matched and the code within the matched blocks. Complex patterns involving multiple property checks might result in slightly increased compile time, but the runtime memory overhead is negligible.
Alternatives
Alternatives to property patterns include:
if-else
statements: These can be used to achieve the same result, but they are often less readable and more verbose.if
statements: Can lead to deeply nested structures that are difficult to maintain.
Pros
Cons
FAQ
-
What version of C# introduced property patterns?
Property patterns were introduced in C# 8.0. -
Can I use property patterns with nullable types?
Yes, you can use property patterns with nullable types. You can use the?
operator to check if a nullable property has a value before matching on its properties. You can also combine it with null pattern for checking that a nullable property is null before anything else. For example :{ Property: null } => "Property is null"
-
Are property patterns performant?
Yes, property patterns are generally performant. However, overly complex patterns can have a performance impact. Keep your patterns concise and avoid unnecessary complexity.