Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Property patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Property Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested property pattern matching
What is the output of this C# code using property patterns?
C Sharp (C#)
record Address(string City, string Country);
record Person(string Name, Address Address);

var person = new Person("Alice", new Address("Paris", "France"));

string result = person is { Address: { City: "Paris" } } ? "Match" : "No match";
Console.WriteLine(result);
ACompilation error
BNo match
CMatch
DRuntime exception
Attempts:
2 left
💡 Hint
Check how nested property patterns match inner properties.
❓ Predict Output
intermediate
2:00remaining
Output of property pattern with discard
What will this C# code print?
C Sharp (C#)
record Product(string Name, decimal Price);

var product = new Product("Book", 12.99m);

string output = product is { Price: > 10, Name: _ } ? "Expensive" : "Cheap";
Console.WriteLine(output);
ACompilation error
BRuntime exception
CCheap
DExpensive
Attempts:
2 left
💡 Hint
The discard (_) means any Name is accepted.
❓ Predict Output
advanced
2:00remaining
Output of property pattern with nested null property
What is the output of this code?
C Sharp (C#)
record Engine(int Horsepower);
record Car(string Model, Engine? Engine);

var car = new Car("Sedan", null);

string result = car is { Engine: { Horsepower: > 200 } } ? "Powerful" : "Not powerful";
Console.WriteLine(result);
ANot powerful
BPowerful
CNullReferenceException
DCompilation error
Attempts:
2 left
💡 Hint
Property patterns safely handle null nested properties.
❓ Predict Output
advanced
2:00remaining
Output of property pattern with multiple conditions
What will this code print?
C Sharp (C#)
record Employee(string Name, int Age, string Department);

var emp = new Employee("John", 30, "HR");

string output = emp is { Age: (>= 18 and <= 65), Department: "HR" } ? "Eligible" : "Not eligible";
Console.WriteLine(output);
ARuntime exception
BEligible
CCompilation error
DNot eligible
Attempts:
2 left
💡 Hint
Check the combined conditions with 'and' in property patterns.
❓ Predict Output
expert
3:00remaining
Output of property pattern with positional and property patterns combined
What is the output of this code?
C Sharp (C#)
record Point(int X, int Y)
{
    public string Quadrant => (X, Y) switch
    {
        (> 0, > 0) => "First",
        (< 0, > 0) => "Second",
        (< 0, < 0) => "Third",
        (> 0, < 0) => "Fourth",
        _ => "Origin or axis"
    };
}

var p = new Point(5, -3);

string result = p is { X: > 0, Y: < 0 } ? p.Quadrant : "Unknown";
Console.WriteLine(result);
AFourth
BFirst
CUnknown
DCompilation error
Attempts:
2 left
💡 Hint
Combine property patterns with positional pattern logic inside the property.

Practice

(1/5)
1. What is the main purpose of using property patterns in C#?
easy
A. To declare new properties in a class
B. To create new objects with default property values
C. To check multiple properties of an object in a clear and concise way
D. To override methods based on property values

Solution

  1. Step 1: Understand what property patterns do

    Property patterns allow checking the values of properties inside an object directly in conditions.
  2. Step 2: Identify the main use

    This helps write clearer and simpler code when you want to check multiple properties without nested ifs.
  3. Final Answer:

    To check multiple properties of an object in a clear and concise way -> Option C
  4. Quick Check:

    Property patterns simplify property checks = B [OK]
Hint: Property patterns simplify checking object properties [OK]
Common Mistakes:
  • Confusing property patterns with property declaration
  • Thinking property patterns create objects
  • Assuming property patterns override methods
2. Which of the following is the correct syntax to use a property pattern in a switch expression for an object person with a property Age?
easy
A. case (Age > 18):
B. case { Age: > 18 }:
C. case Age > 18:
D. case person.Age > 18:

Solution

  1. Step 1: Recall property pattern syntax

    Property patterns use curly braces with property name and condition inside, like { Age: > 18 }.
  2. Step 2: Match with options

    case { Age: > 18 }: matches this syntax exactly, others use invalid or incorrect forms.
  3. Final Answer:

    case { Age: > 18 }: -> Option B
  4. Quick Check:

    Property pattern syntax uses braces = A [OK]
Hint: Use braces with property name and condition for property patterns [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Writing property condition without braces
  • Referencing object name inside case
3. What will be the output of the following code?
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);
medium
A. Adult
B. Minor
C. Senior
D. Compilation error

Solution

  1. Step 1: Check the Age property of the Person object

    The person has Age = 25.
  2. 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".
  3. Final Answer:

    Adult -> Option A
  4. Quick Check:

    Age 25 fits adult range = A [OK]
Hint: Match property values carefully in switch cases [OK]
Common Mistakes:
  • Confusing range conditions
  • Assuming default case runs first
  • Ignoring property pattern syntax
4. Identify the error in the following code using property patterns:
record Product(string Name, decimal Price);

Product p = new("Book", 15.99m);

if (p is { Price > 10 })
{
    Console.WriteLine("Expensive");
}
medium
A. The if statement requires parentheses around the pattern
B. The decimal literal should not have 'm' suffix
C. The record declaration is missing a semicolon
D. The property pattern syntax is incorrect; it should be { Price: > 10 }

Solution

  1. Step 1: Check property pattern syntax

    Property patterns require a colon between property name and condition, like { Price: > 10 }.
  2. Step 2: Identify the error in the code

    The code uses { Price > 10 } which is invalid syntax.
  3. Final Answer:

    The property pattern syntax is incorrect; it should be { Price: > 10 } -> Option D
  4. Quick Check:

    Property patterns need colon between property and condition = D [OK]
Hint: Use colon between property and condition in patterns [OK]
Common Mistakes:
  • Omitting colon in property pattern
  • Misunderstanding decimal literal suffix
  • Adding unnecessary parentheses
5. Given a list of 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?
hard
A. persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: >= 13 and <= 19 } => "Teen", { Age: >= 20 and <= 64 } => "Adult", _ => "Senior" })
B. persons.Select(p => p switch { Age < 13 => "Child", Age >= 13 and Age <= 19 => "Teen", Age >= 20 and Age <= 64 => "Adult", _ => "Senior" })
C. persons.Select(p => p switch { { Age < 13 } => "Child", { Age >= 13 and Age <= 19 } => "Teen", { Age >= 20 and Age <= 64 } => "Adult", _ => "Senior" })
D. persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: > 13 and < 20 } => "Teen", { Age: >= 20 and < 65 } => "Adult", _ => "Senior" })

Solution

  1. Step 1: Understand correct property pattern syntax with ranges

    Property patterns use colon and conditions like { Age: < 13 } and combine with and for ranges.
  2. 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).
  3. Final Answer:

    persons.Select(p => p switch { { Age: < 13 } => "Child", { Age: >= 13 and <= 19 } => "Teen", { Age: >= 20 and <= 64 } => "Adult", _ => "Senior" }) -> Option A
  4. Quick Check:

    Correct property pattern syntax with proper ranges = C [OK]
Hint: Use braces and colon with 'and' for combined property conditions [OK]
Common Mistakes:
  • Omitting braces or colon in property patterns
  • Using incorrect range operators or missing boundaries
  • Mixing syntax from other languages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard