Pattern matching — test expressions against patterns and extract values.
Type patterns:
1if (obj is string s)2 Console.WriteLine(s.Length);34if (obj is not null)5 Process(obj);
Property patterns:
1if (person is { Age: > 18, Name: not null })2 ProcessAdult(person);
Switch expressions:
1var result = shape switch2{3 Circle { Radius: > 0 } c => c.Area,4 Rectangle { Width: var w, Height: var h } => w * h,5 _ => 06};
Relational patterns (C# 9):
1static string Classify(int value) => value switch2{3 < 0 => "negative",4 0 => "zero",5 > 0 and < 100 => "small",6 >= 100 => "large"7};
Logical patterns (and, or, not):
1if (score is >= 90 and <= 100)2 Console.WriteLine("A");