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)
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.