csharp / expert
Snippet
Multi-Stage Switch Guards
Expert-level control flow using tuple patterns combined with property guards (when clauses). This allows for complex logic within a concise expression without nesting if-statements.
snippet.cs
csharp
1
2
3
4
5
6
7
public string GetDiscountDescription(int score, bool isVip) => (score, isVip) switch{(> 90, true) => "Elite VIP Discount",(> 90, false) => "High Score Discount",(> 50, _) when score % 2 == 0 => "Even Score Bonus",_ => "Standard Rate"};
Breakdown
1
(score, isVip) switch
Creates a temporary tuple to match against multiple variables simultaneously.
2
(> 50, _) when score % 2 == 0
Uses a wildcard for the second element and a custom boolean guard for the first.