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

Pattern matching in switch in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pattern matching in switch
Input value
Switch statement
Check case patterns one by one
Match found?
NoDefault case or exit
Yes
Execute matched case block
End switch
The switch statement takes an input and checks it against multiple patterns. When a pattern matches, it runs the corresponding code block and then exits.
Execution Sample
C Sharp (C#)
object obj = 42;
switch (obj)
{
    case int i:
        Console.WriteLine($"Integer: {i}");
        break;
    case string s:
        Console.WriteLine($"String: {s}");
        break;
    default:
        Console.WriteLine("Unknown type");
        break;
}
This code checks the type of obj and prints a message depending on whether it is an int, a string, or something else.
Execution Table
StepInput ValuePattern CheckedMatch ResultAction TakenOutput
142 (int)case int iMatchPrint Integer: 42Integer: 42
242 (int)case string sNot checked (already matched)No action
342 (int)defaultNot checked (already matched)No action
4EndSwitch endsExitSwitch ends
💡 Pattern matched at step 1, switch exits after executing matched case.
Variable Tracker
VariableStartAfter Step 1After Step 4
obj42 (int)42 (int)42 (int)
i (int)undefined4242
s (string)undefinedundefinedundefined
Key Moments - 3 Insights
Why does the switch stop checking other cases after a match?
Once a pattern matches (see execution_table step 1), the switch executes that case and exits to avoid running multiple cases.
What happens if no pattern matches the input?
If no pattern matches, the default case runs (if present). In the example, default is not reached because int matches first.
How does pattern matching extract variables like 'i'?
When a pattern matches, the matched value is assigned to the variable (e.g., 'i' gets 42 at step 1), so you can use it inside the case block.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' after step 1?
Aundefined
B42
Cnull
Dstring value
💡 Hint
Check variable_tracker row for 'i' after step 1.
At which step does the switch stop checking further patterns?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
See execution_table where match is found and switch exits.
If obj was a string "hello", which pattern would match?
Adefault
Bcase int i
Ccase string s
DNo match
💡 Hint
Look at the patterns and their types in the code sample.
Concept Snapshot
switch (value) {
  case Type varName: // matches type and assigns
    // code block
    break;
  default:
    // fallback code
    break;
}
Pattern matching checks each case pattern in order and runs the first matching block.
Full Transcript
Pattern matching in switch lets you check the type or shape of a value and run code based on that. The switch statement tests each pattern one by one. When a pattern matches, it assigns the matched value to a variable and runs the case block. Then it stops checking further cases. If no pattern matches, the default case runs if present. This helps write clear and concise code that reacts to different data types or conditions.