Skip to content

C# Feature - Constant patterns

Constant patterns let you match against literal values or named constants with the is operator or inside switch expressions.

C# 7.0 (2017)

if (status == "Ready")
{
Start();
}
if (status is "Ready")
{
Start();
}
string message = responseCode switch
{
200 => "OK",
404 => "Missing",
_ => "Other"
};
  • Prefer constant patterns inside broader pattern matching code to keep style consistent.
  • Use the discard pattern _ for the fallback branch.
  • C# 7 introduced constant and type patterns.