C# Feature - Relational patterns
Overview
Section titled “Overview”Relational patterns let you express numeric or comparable thresholds directly in a pattern match instead of nested comparisons.
Introduced In
Section titled “Introduced In”C# 9.0 (2020)
Before
Section titled “Before”if (score >= 90){ return "A";}return score switch{ >= 90 => "A", >= 80 => "B", _ => "C"};Annotated Example
Section titled “Annotated Example”bool isWorkingHours = currentHour is >= 9 and < 18;Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Relational patterns become more readable when paired with logical patterns such as
and. - Keep branches ordered from most specific to most general.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- C# 9 added relational and logical pattern combinations.