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

Pattern matching in switch in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pattern Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pattern matching with type check
What is the output of this C# code using pattern matching in a switch expression?
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"
};
System.Console.WriteLine(result);
AString
BInteger
COther
DGreater than 40
Attempts:
2 left
💡 Hint
Look at the order of patterns and the value of obj.
Predict Output
intermediate
2:00remaining
Pattern matching with property check in switch
What will this C# code print when using pattern matching on a record type?
C Sharp (C#)
record Person(string Name, int Age);

Person p = new("Alice", 30);
string category = p switch
{
    { Age: < 18 } => "Minor",
    { Age: >= 18 and < 65 } => "Adult",
    { Age: >= 65 } => "Senior",
    _ => "Unknown"
};
System.Console.WriteLine(category);
AMinor
BAdult
CSenior
DUnknown
Attempts:
2 left
💡 Hint
Check the Age property value and which pattern it matches.
🔧 Debug
advanced
2:00remaining
Identify the error in pattern matching switch
What error does this C# code produce when compiled?
C Sharp (C#)
object value = "hello";
switch (value)
{
    case int i:
        System.Console.WriteLine("Integer");
        break;
    case string s when s.Length > 5:
        System.Console.WriteLine("Long string");
        break;
    case string s:
        System.Console.WriteLine("String");
        break;
    case null:
        System.Console.WriteLine("Null");
        break;
}
ANo error, code compiles and runs
BCS0161: Not all code paths return a value
CCS0152: The switch cases are unreachable due to order
DCS8120: A pattern is unreachable because a previous pattern matches all strings
Attempts:
2 left
💡 Hint
Look at the order of string cases and their conditions.
📝 Syntax
advanced
2:00remaining
Which switch pattern syntax is invalid?
Which of these C# switch case patterns will cause a syntax error?
Acase int i when i % 2 == 0:
Bcase { Length: > 5 } s:
Ccase string s if s.StartsWith("A"):
Dcase null:
Attempts:
2 left
💡 Hint
Check the correct keyword for pattern guards in switch cases.
🚀 Application
expert
3:00remaining
Determine the final value after complex pattern matching
Given the following C# code, what is the value of 'result' after execution?
C Sharp (C#)
object data = new List<int> { 1, 2, 3 };
string result = data switch
{
    List<int> list when list.Count == 0 => "Empty list",
    List<int> list when list.Count > 0 && list[0] == 1 => "Starts with one",
    List<int> list => "Other list",
    _ => "Unknown"
};
A"Starts with one"
B"Empty list"
C"Other list"
D"Unknown"
Attempts:
2 left
💡 Hint
Check the list contents and which pattern matches first.