0
0
C Sharp (C#)programming~5 mins

Property patterns in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a property pattern in C#?
A property pattern lets you check if an object has certain properties with specific values or conditions, making it easier to match complex data shapes in a clear way.
Click to reveal answer
beginner
How do you write a simple property pattern to check if a Point object has X equal to 0?
You write: point is { X: 0 }. This means the object 'point' has a property 'X' with value 0.
Click to reveal answer
intermediate
Can property patterns check nested properties? Give an example.
Yes. For example: person is { Address: { City: "Paris" } } checks if 'person' has an 'Address' property whose 'City' is "Paris".
Click to reveal answer
intermediate
What happens if a property in a property pattern is null or missing?
The pattern does not match. Property patterns require the property to exist and meet the condition; otherwise, the match fails.
Click to reveal answer
intermediate
How can you combine multiple property patterns in one check?
You can list multiple properties inside braces, like obj is { Prop1: 5, Prop2: "hello" }, which means both conditions must be true.
Click to reveal answer
What does the pattern obj is { Name: "Alice" } check?
AIf obj has any property named Name
BIf obj is a string "Alice"
CIf obj is null
DIf obj has a property Name equal to "Alice"
Can property patterns be used to check nested properties?
AOnly for properties of type int
BNo, only top-level properties
CYes, by nesting braces for each property level
DOnly if the object is a struct
What happens if a property in the pattern is missing on the object?
AThe pattern does not match
BThe pattern matches anyway
CThe pattern throws an exception
DThe property is ignored
How do you check multiple properties in one property pattern?
AList properties separated by commas inside braces
BUse multiple 'is' keywords
CUse && operator between patterns
DUse a switch statement
Which C# version introduced property patterns?
AC# 6
BC# 9
CC# 8
DC# 7
Explain what a property pattern is and how it helps in checking object properties.
Think about how you check if an object has certain values inside it.
You got /3 concepts.
    Describe how to write a property pattern that checks multiple properties and nested properties.
    Imagine checking a person's address city inside the person object.
    You got /3 concepts.