Concept Flow - Property patterns
Start with an object
Check property values
Match pattern?
Execute true
End
Property patterns check if an object's properties match specified values, then decide the flow based on the match.
record Person(string Name, int Age); var person = new Person("Alice", 30); if (person is { Name: "Alice", Age: 30 }) Console.WriteLine("Match found"); else Console.WriteLine("No match");
| Step | Expression Evaluated | Property Checked | Value Found | Condition Result | Branch Taken | Output |
|---|---|---|---|---|---|---|
| 1 | person is { Name: "Alice", Age: 30 } | Name | "Alice" | Matches | Continue | |
| 2 | person is { Name: "Alice", Age: 30 } | Age | 30 | Matches | Continue | |
| 3 | if condition | N/A | N/A | True | True branch | Match found |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| person.Name | "Alice" | "Alice" | "Alice" | "Alice" |
| person.Age | 30 | 30 | 30 | 30 |
Property patterns check if an object's properties match given values.
Syntax: obj is { Prop1: val1, Prop2: val2 }
All specified properties must match for true.
Used in if, switch, or expressions.
Simplifies checking multiple properties at once.