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

Why pattern matching matters in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pattern Matching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pattern matching with 'is' and 'switch'

What is the output of this C# code using pattern matching with is and switch?

C Sharp (C#)
object obj = 42;
string result = obj switch
{
    int i when i > 40 => "Greater than 40",
    int i => "Integer",
    string s => "String",
    _ => "Other"
};
Console.WriteLine(result);
AOther
BInteger
CString
DGreater than 40
Attempts:
2 left
💡 Hint

Look at the when condition in the switch expression.

🧠 Conceptual
intermediate
1:30remaining
Why use pattern matching instead of if-else?

Which reason best explains why pattern matching is preferred over multiple if-else statements in C#?

APattern matching automatically fixes bugs in the code.
BPattern matching runs faster than any <code>if-else</code> statement in all cases.
CPattern matching allows clearer, more readable code by combining type checks and conditions in one expression.
DPattern matching can only be used with strings, making it simpler.
Attempts:
2 left
💡 Hint

Think about code readability and combining checks.

🔧 Debug
advanced
2:00remaining
Identify the error in pattern matching syntax

What error does this C# code produce?

C Sharp (C#)
object value = "hello";
switch (value)
{
    case string s && s.Length > 3:
        Console.WriteLine("Long string");
        break;
    default:
        Console.WriteLine("Other");
        break;
}
ASyntaxError: '&&' cannot be used in case patterns
BRuntimeException: NullReferenceException
CNo error, prints 'Long string'
DCompileError: Missing colon after case
Attempts:
2 left
💡 Hint

Check the syntax for combining conditions in case patterns.

Predict Output
advanced
2:00remaining
Output of nested pattern matching with tuples

What is the output of this C# code using nested pattern matching with tuples?

C Sharp (C#)
var point = (X: 0, Y: 5);
string quadrant = point switch
{
    (0, 0) => "Origin",
    (0, var y) when y > 0 => "On positive Y axis",
    (var x, 0) when x > 0 => "On positive X axis",
    (var x, var y) when x > 0 && y > 0 => "Quadrant 1",
    _ => "Other"
};
Console.WriteLine(quadrant);
AOn positive Y axis
BOn positive X axis
CQuadrant 1
DOrigin
Attempts:
2 left
💡 Hint

Look at the tuple values and the matching patterns carefully.

🚀 Application
expert
2:30remaining
Determine the number of matched cases in a pattern matching method

Given this method, how many times will the Console.WriteLine print "Matched" when called with the array new object[] { 5, "test", null, 10 }?

C Sharp (C#)
void CheckObjects(object[] items)
{
    foreach (var item in items)
    {
        if (item is int i && i > 5)
            Console.WriteLine("Matched");
    }
}

CheckObjects(new object[] { 5, "test", null, 10 });
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Count how many integers in the array are greater than 5.