csharp / intermediate
Snippet
Object Classification via Positional Patterns
Positional patterns allow you to deconstruct an object and match against its parts. This is highly effective for state-based logic and replaces long if-else chains with readable switch expressions.
snippet.cs
csharp
1
2
3
4
5
6
7
var result = person switch{("Admin", true) => "Full Access",("User", true) => "Standard Access",(_, false) => "Account Disabled",_ => "Unknown Role"};
Breakdown
1
("Admin", true) => "Full Access"
Matches if the deconstructed object has the role 'Admin' and is active.
2
(_, false) => "Account Disabled"
The discard (_) matches any role as long as the second property is false.