C# Feature - Constant patterns
Overview
Section titled βOverviewβConstant patterns let you match against literal values or named constants with the is operator or inside switch expressions.
Introduced In
Section titled βIntroduced InβC# 7.0 (2017)
if (status == "Ready"){ Start();}if (status is "Ready"){ Start();}Annotated Example
Section titled βAnnotated Exampleβstring message = responseCode switch{ 200 => "OK", 404 => "Missing", _ => "Other"};Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Prefer constant patterns inside broader pattern matching code to keep style consistent.
- Use the discard pattern
_for the fallback branch.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- C# 7 introduced constant and type patterns.