Property patterns help you check if an object has certain values in its properties easily and clearly.
Property patterns in C Sharp (C#)
obj is { Property1: value1, Property2: value2, ... }You use is keyword followed by curly braces with property names and values.
This checks if obj has properties matching the given values.
person object has a Name property equal to "Alice".if (person is { Name: "Alice" }) { Console.WriteLine("Hello Alice!"); }
point has X and Y properties both equal to 0.if (point is { X: 0, Y: 0 }) { Console.WriteLine("Point is at the origin."); }
Quantity is greater than 0.if (order is { Status: "Shipped", Quantity: > 0 }) { Console.WriteLine("Order is shipped and has items."); }
This program creates a Person object and uses a property pattern to check if the Name is "Bob" and Age is 30. It prints a message if both match.
using System; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Person person = new() { Name = "Bob", Age = 30 }; if (person is { Name: "Bob", Age: 30 }) { Console.WriteLine("Found Bob who is 30 years old."); } else { Console.WriteLine("Person does not match."); } } }
Property patterns work well with records and classes that have readable properties.
You can combine property patterns with other patterns like relational patterns for more checks.
Property patterns let you check object properties easily and clearly.
Use them to simplify conditions that check multiple properties.
They improve code readability and reduce nested if statements.
Practice
property patterns in C#?Solution
Step 1: Understand what property patterns do
Property patterns allow checking the values of properties inside an object directly in conditions.Step 2: Identify the main use
This helps write clearer and simpler code when you want to check multiple properties without nested ifs.Final Answer:
To check multiple properties of an object in a clear and concise way -> Option CQuick Check:
Property patterns simplify property checks = B [OK]
- Confusing property patterns with property declaration
- Thinking property patterns create objects
- Assuming property patterns override methods
switch expression for an object person with a property Age?Solution
Step 1: Recall property pattern syntax
Property patterns use curly braces with property name and condition inside, like{ Age: > 18 }.Step 2: Match with options
case { Age: > 18 }: matches this syntax exactly, others use invalid or incorrect forms.Final Answer:
case { Age: > 18 }: -> Option BQuick Check:
Property pattern syntax uses braces = A [OK]
- Using parentheses instead of braces
- Writing property condition without braces
- Referencing object name inside case
record Person(string Name, int Age);
Person p = new("Alice", 25);
string result = p switch
{
{ Age: < 18 } => "Minor",
{ Age: >= 18 and < 65 } => "Adult",
_ => "Senior"
};
Console.WriteLine(result);Solution
Step 1: Check the Age property of the Person object
The person has Age = 25.Step 2: Match Age with switch cases
25 is not less than 18, so first case fails. 25 is >= 18 and < 65, so second case matches and returns "Adult".Final Answer:
Adult -> Option AQuick Check:
Age 25 fits adult range = A [OK]
- Confusing range conditions
- Assuming default case runs first
- Ignoring property pattern syntax
record Product(string Name, decimal Price);
Product p = new("Book", 15.99m);
if (p is { Price > 10 })
{
Console.WriteLine("Expensive");
}Solution
Step 1: Check property pattern syntax
Property patterns require a colon between property name and condition, like{ Price: > 10 }.Step 2: Identify the error in the code
The code uses{ Price > 10 }which is invalid syntax.Final Answer:
The property pattern syntax is incorrect; it should be { Price: > 10 } -> Option DQuick Check:
Property patterns need colon between property and condition = D [OK]
- Omitting colon in property pattern
- Misunderstanding decimal literal suffix
- Adding unnecessary parentheses
Person objects, how can you use property patterns in a switch expression to classify each person as "Child" if Age < 13, "Teen" if Age between 13 and 19, "Adult" if Age between 20 and 64, and "Senior" otherwise? Which code snippet correctly implements this?Solution
Step 1: Understand correct property pattern syntax with ranges
Property patterns use colon and conditions like{ Age: < 13 }and combine withandfor ranges.Step 2: Check each option for correct syntax and ranges
persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: >= 13 and <= 19 } => "Teen", { Age: >= 20 and <= 64 } => "Adult", _ => "Senior" }) uses correct syntax and inclusive ranges matching the problem. persons.Select(p => p switch { Age < 13 => "Child", Age >= 13 and Age <= 19 => "Teen", Age >= 20 and Age <= 64 => "Adult", _ => "Senior" }) lacks braces and colons. persons.Select(p => p switch { { Age < 13 } => "Child", { Age >= 13 and Age <= 19 } => "Teen", { Age >= 20 and Age <= 64 } => "Adult", _ => "Senior" }) misplaces comparison operators inside braces. persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: > 13 and < 20 } => "Teen", { Age: >= 20 and < 65 } => "Adult", _ => "Senior" }) uses exclusive Teen range (should include 13 and 19).Final Answer:
persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: >= 13 and <= 19 } => "Teen", { Age: >= 20 and <= 64 } => "Adult", _ => "Senior" }) -> Option AQuick Check:
Correct property pattern syntax with proper ranges = C [OK]
- Omitting braces or colon in property patterns
- Using incorrect range operators or missing boundaries
- Mixing syntax from other languages
Ready to prove it?
Pass the test and this pattern moves unknown
