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

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

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

Practice

(1/5)
1. What does pattern matching in a switch statement primarily help you do in C#?
easy
A. Check an object's type and properties in a clear way
B. Loop through collections more efficiently
C. Declare variables with less code
D. Handle exceptions automatically

Solution

  1. Step 1: Understand pattern matching purpose

    Pattern matching in switch lets you test an object's type and properties clearly.
  2. Step 2: Compare with other options

    Looping, variable declaration, and exception handling are unrelated to pattern matching in switch.
  3. Final Answer:

    Check an object's type and properties in a clear way -> Option A
  4. Quick Check:

    Pattern matching = type and property checks [OK]
Hint: Pattern matching checks types and conditions clearly [OK]
Common Mistakes:
  • Confusing pattern matching with loops
  • Thinking it handles exceptions
  • Believing it declares variables automatically
2. Which of the following is the correct syntax to match an integer variable num equal to 10 using pattern matching in a switch expression?
easy
A. case int n when n == 10:
B. case int n == 10:
C. case int n => 10:
D. case int n is 10:

Solution

  1. Step 1: Recall correct pattern matching syntax

    To match a value with a condition, use case Type variable when condition:.
  2. Step 2: Check each option

    case int n when n == 10: uses when n == 10, which is correct. Others use invalid syntax.
  3. Final Answer:

    case int n when n == 10: -> Option A
  4. Quick Check:

    Use 'when' for conditions in pattern matching [OK]
Hint: Use 'when' keyword for conditions in switch patterns [OK]
Common Mistakes:
  • Using '==' directly after case without 'when'
  • Using '=>' or 'is' incorrectly in case
  • Omitting the variable name
3. What will be the output of this C# code?
object obj = 42;
switch (obj)
{
    case int n when n > 40:
        Console.WriteLine("Greater than 40");
        break;
    case int n:
        Console.WriteLine("Integer");
        break;
    default:
        Console.WriteLine("Other");
        break;
}
medium
A. Integer
B. Other
C. Greater than 40
D. No output (compile error)

Solution

  1. Step 1: Identify the object type and value

    obj is an integer with value 42.
  2. Step 2: Evaluate switch cases in order

    First case matches int n when n > 40, which is true for 42, so prints "Greater than 40" and breaks.
  3. Final Answer:

    Greater than 40 -> Option C
  4. Quick Check:

    Pattern with 'when' matched first [OK]
Hint: Check cases top to bottom; first match runs [OK]
Common Mistakes:
  • Ignoring the 'when' condition
  • Assuming second case runs first
  • Thinking default runs for int
4. Identify the error in this switch statement using pattern matching:
object val = "hello";
switch (val)
{
    case string s when s.Length > 3
        Console.WriteLine("Long string");
        break;
    default:
        Console.WriteLine("Other");
        break;
}
medium
A. Cannot use 'when' with string type
B. Missing colon ':' after case pattern
C. Missing parentheses around condition
D. Incorrect variable name in case

Solution

  1. Step 1: Check switch case syntax

    Each case pattern must end with a colon ':' before the statements.
  2. Step 2: Locate missing colon

    The case line lacks ':' after when s.Length > 3, causing syntax error.
  3. Final Answer:

    Missing colon ':' after case pattern -> Option B
  4. Quick Check:

    Case patterns need ':' after condition [OK]
Hint: Always put ':' after case pattern line [OK]
Common Mistakes:
  • Forgetting ':' after case pattern
  • Adding parentheses around 'when' condition
  • Thinking 'when' can't be used with strings
5. Given a list of objects items containing integers and strings, how would you use pattern matching in a switch expression to return a new list of strings where:
- Integers greater than 10 become "Big number"
- Strings longer than 5 characters become "Long text"
- All others become "Other"?

Which code snippet correctly implements this?
hard
A. items.Select(item => item switch { int n when n > 10: "Big number", string s when s.Length > 5: "Long text", _ => "Other" }).ToList();
B. items.Select(item => switch (item) { case int n if n > 10: "Big number"; case string s if s.Length > 5: "Long text"; default: "Other"; }).ToList();
C. items.Select(item => item switch { int n if n > 10: "Big number", string s if s.Length > 5: "Long text", _ => "Other" }).ToList();
D. items.Select(item => item switch { int n when n > 10 => "Big number", string s when s.Length > 5 => "Long text", _ => "Other" }).ToList();

Solution

  1. Step 1: Understand switch expression syntax

    Switch expressions use '=>' for cases, and 'when' for conditions.
  2. Step 2: Check each option's syntax

    items.Select(item => item switch { int n when n > 10 => "Big number", string s when s.Length > 5 => "Long text", _ => "Other" }).ToList(); uses correct 'item switch { case => result }' with 'when' and '=>'. Other options use invalid syntax ('switch' inside lambda or 'if' instead of 'when'). items.Select(item => item switch { int n when n > 10: "Big number", string s when s.Length > 5: "Long text", _ => "Other" }).ToList(); uses ':' instead of '=>', which is invalid in switch expressions.
  3. Final Answer:

    items.Select(item => item switch { int n when n > 10 => "Big number", string s when s.Length > 5 => "Long text", _ => "Other" }).ToList(); -> Option D
  4. Quick Check:

    Switch expressions use '=>' and 'when' [OK]
Hint: Use '=>' and 'when' in switch expressions for conditions [OK]
Common Mistakes:
  • Using ':' instead of '=>' in switch expressions
  • Using 'if' instead of 'when' for conditions
  • Trying to put 'switch' keyword inside lambda
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