Skip to content

C# Feature - Relational patterns

Relational patterns let you express numeric or comparable thresholds directly in a pattern match instead of nested comparisons.

C# 9.0 (2020)

if (score >= 90)
{
return "A";
}
return score switch
{
>= 90 => "A",
>= 80 => "B",
_ => "C"
};
bool isWorkingHours = currentHour is >= 9 and < 18;
  • Relational patterns become more readable when paired with logical patterns such as and.
  • Keep branches ordered from most specific to most general.
  • C# 9 added relational and logical pattern combinations.