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

Positional patterns in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Positional patterns
Input value
Match with positional pattern
Extract parts
Use extracted parts
Execute matched case block
Output or result
Positional patterns check the shape of data and extract parts to use in code, then run matching case blocks.
Execution Sample
C Sharp (C#)
record Point(int X, int Y);

Point p = new(3, 4);

string result = p 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})"
};
This code matches a Point record with positional patterns and returns a string describing its position.
Execution Table
StepInputPattern MatchedExtracted VariablesActionOutput
1Point(3,4)(0,0)noneNo match, try next
2Point(3,4)(var x, 0)x=3Y is not 0, no match
3Point(3,4)(0, var y)y=4X is not 0, no match
4Point(3,4)(var x, var y)x=3, y=4Match, create stringPoint at (3,4)
💡 All patterns checked; last pattern matches and execution stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
pPoint(3,4)Point(3,4)Point(3,4)Point(3,4)Point(3,4)Point(3,4)
xundefinedundefined3undefined33
yundefinedundefinedundefined444
resultundefinedundefinedundefinedundefinedPoint at (3,4)Point at (3,4)
Key Moments - 2 Insights
Why does the pattern (var x, 0) not match when Y is 4?
Because the pattern requires the second value to be exactly 0, but Y is 4, so it fails as shown in step 2 of the execution_table.
What happens if no positional pattern matches?
The switch expression would not compile (non-exhaustive patterns); here, the last pattern (var x, var y) matches anything, ensuring a result as in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'x' when the pattern (var x, 0) is checked?
A3
B0
Cundefined
D4
💡 Hint
Check the 'Extracted Variables' column at step 2 in the execution_table.
At which step does the pattern match successfully?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Pattern Matched' and 'Action' columns to see when the match happens.
If the point was (0,0), which pattern would match first?
A(var x, var y)
B(var x, 0)
C(0, 0)
D(0, var y)
💡 Hint
Refer to the order of patterns in the code and the execution_table logic.
Concept Snapshot
Positional patterns match data by shape and extract parts.
Syntax: (var1, var2, ...)
Used in switch or is expressions.
Matches in order; first match runs.
Extracted variables can be used in code block.
Full Transcript
This example shows how positional patterns in C# match a Point record's X and Y values. The switch expression tries each pattern in order. It first checks if the point is at the origin (0,0). If not, it checks if Y is zero, then if X is zero. Finally, it matches any point and extracts X and Y. Variables x and y get values only when their pattern matches. The output string describes the point's position based on the matched pattern.