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

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

Choose your learning style9 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.