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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Positional Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of positional pattern matching with tuples
What is the output of this C# code using positional patterns with tuples?
C Sharp (C#)
var point = (X: 3, Y: 4);
string result = point switch
{
    (0, 0) => "Origin",
    (var x, 0) => $"X={x}, on X-axis",
    (0, var y) => $"Y={y}, on Y-axis",
    (var x, var y) => $"Point at ({x},{y})"
};
Console.WriteLine(result);
AX=3, on X-axis
BY=4, on Y-axis
CPoint at (3,4)
DOrigin
Attempts:
2 left
💡 Hint
Think about which pattern matches the tuple (3,4) first.
Predict Output
intermediate
2:00remaining
Pattern matching with positional records
Given the record and code below, what is the output?
C Sharp (C#)
record Person(string FirstName, string LastName);

var person = new Person("Alice", "Smith");
string greeting = person switch
{
    ("Alice", "Smith") => "Hello Alice Smith!",
    ("Bob", _) => "Hello Bob!",
    _ => "Hello stranger!"
};
Console.WriteLine(greeting);
AHello Alice Smith!
BHello Bob!
CHello stranger!
DCompilation error
Attempts:
2 left
💡 Hint
Check which positional pattern matches the Person record.
🔧 Debug
advanced
2:00remaining
Positional pattern matching with guards on records
What is the output of this code?
C Sharp (C#)
record Point(int X, int Y);

var p = new Point(1, 2);
string result = p switch
{
    (var x, var y) when x > y => "X is greater",
    (var x, var y) when y > x => "Y is greater",
    (var x, var y) when x == y => "Equal",
    _ => "Unknown"
};
Console.WriteLine(result);
AOutput: Y is greater
BCompilation error: positional patterns not supported on records
CRuntime error: NullReferenceException
DOutput: X is greater
Attempts:
2 left
💡 Hint
Records support positional patterns via generated Deconstruct methods. For (1,2), evaluate the when conditions in order.
Predict Output
advanced
2:00remaining
Output of nested positional patterns
What is the output of this code with nested positional patterns?
C Sharp (C#)
record Rectangle(Point TopLeft, Point BottomRight);
record Point(int X, int Y);

var rect = new Rectangle(new Point(0, 0), new Point(5, 5));
string description = rect switch
{
    (var (x1, y1), var (x2, y2)) when x1 == 0 && y1 == 0 && x2 == 5 && y2 == 5 => "Standard 5x5 rectangle",
    _ => "Other rectangle"
};
Console.WriteLine(description);
ARuntime error
BOther rectangle
CCompilation error
DStandard 5x5 rectangle
Attempts:
2 left
💡 Hint
Look at how nested positional patterns match the record properties.
Predict Output
expert
3:00remaining
Output of positional pattern with deconstruction and discard
What is the output of this code using positional patterns with discard (_) and deconstruction?
C Sharp (C#)
record Color(int R, int G, int B);

Color c = new(255, 0, 0);
string result = c switch
{
    (255, 0, 0) => "Red",
    (0, 255, 0) => "Green",
    (0, 0, 255) => "Blue",
    (var r, var g, var b) when r == g && g == b => "Gray",
    _ => "Unknown color"
};
Console.WriteLine(result);
AGray
BRed
CUnknown color
DGreen
Attempts:
2 left
💡 Hint
Check which positional pattern matches the color (255,0,0).