Skip to content

Constant pattern

Context: The constant pattern tests whether an expression equals a constant value (literal, null, or const).

int value = 5;
if (value is 5)
{
Console.WriteLine("It's five");
}
// In switch expression
string result = value switch
{
1 => "One",
2 => "Two",
_ => "Other"
};

Command handling: Use constant pattern to match specific command types in a switch expression (e.g., case "CREATE": ...).

Example: In a TCP server, you might parse string commands and use constant pattern.