The constant pattern in C# is a pattern matching construct used to test whether a given expression evaluates to a specific, compile-time constant value. Semantically, the pattern evaluates toDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
true if object.Equals(constant, expression) returns true, providing a type-safe equality check between the runtime value of the expression and the specified constant.
Allowed Constant Expressions
The pattern requires the right-hand side of the match to be a compile-time constant. Valid constant expressions include:- Numeric literals (
int,float,double, etc.) - Character and string literals (
'A',"Text") - Boolean literals (
true,false) - Enum values
- Declared
constfields - The
nullkeyword defaultvalue expressions (e.g.,default(T))
Syntax
The constant pattern is primarily implemented using theis operator or within switch statements and switch expressions.
Execution Semantics and Equality Resolution
The C# language specification strictly defines the semantics of the constant pattern as matching ifobject.Equals(constant, expression) evaluates to true. However, the compiler applies specific rules for type safety, implicit conversions, and Intermediate Language (IL) optimizations:
- Implicit Conversions: When matching primitive numeric types, the constant value must be implicitly convertible to the type of the input expression. If no implicit conversion exists from the constant’s type to the expression’s type, the compiler generates an error.
- IL Optimizations: While the semantic definition relies on
object.Equals, the compiler optimizes the emitted IL based on the operand types to avoid boxing overhead where possible:- Primitive Numerics and Enums: The compiler avoids calling
object.Equals. For boxed primitives, it emits anisinsttype check, unboxes the value, and performs a direct value comparison (e.g., theceqinstruction). - Complex Value Types: For types like
decimalor custom structs (e.g., matching againstdefault(MyStruct)), the compiler cannot use a simpleceqinstruction. It emits calls todecimal.Equals,decimal.op_Equality,IEquatable<T>.Equals, orobject.Equalsto ensure correct semantic evaluation. - Strings: The compiler emits a call to
string.Equals.
- Primitive Numerics and Enums: The compiler avoids calling
- Floating-Point NaN: Because the constant pattern adheres to
object.Equalssemantics, it handlesNaN(Not-a-Number) values differently than the standard IEEE 754 equality rules applied by the==operator. Whilex == double.NaNalways evaluates tofalse, the patternx is double.NaNevaluates totrueif the runtime value ofxisNaN. - Null Checking: The
nullconstant pattern (expr is null) is a specialized form optimized to a direct reference equality check against null. It strictly bypasses any overloaded==operators on the type to guarantee an accurate, un-hijackable null check (equivalent toobject.ReferenceEquals(expr, null)).
Master C# with Deep Grasping Methodology!Learn More





