> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Constant Pattern

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 to `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 `const` fields
* The `null` keyword
* `default` value expressions (e.g., `default(T)`)

## Syntax

The constant pattern is primarily implemented using the `is` operator or within `switch` statements and `switch` expressions.

```csharp theme={"dark"}
object input = 42;
const int TargetValue = 42;

// Constant pattern using a literal
bool isZero = input is 0; 

// Constant pattern using a const field
bool isTarget = input is TargetValue; 

// Constant pattern within a switch expression
string EvaluateStatus(int statusCode) => statusCode switch
{
    200 => "OK",           // Constant pattern (integer literal)
    404 => "Not Found",    // Constant pattern (integer literal)
    _ => "Unknown"         // Discard pattern
};
```

## Execution Semantics and Equality Resolution

The C# language specification strictly defines the semantics of the constant pattern as matching if `object.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 an `isinst` type check, unboxes the value, and performs a direct value comparison (e.g., the `ceq` instruction).
  * **Complex Value Types:** For types like `decimal` or custom structs (e.g., matching against `default(MyStruct)`), the compiler cannot use a simple `ceq` instruction. It emits calls to `decimal.Equals`, `decimal.op_Equality`, `IEquatable<T>.Equals`, or `object.Equals` to ensure correct semantic evaluation.
  * **Strings:** The compiler emits a call to `string.Equals`.
* **Floating-Point NaN:** Because the constant pattern adheres to `object.Equals` semantics, it handles `NaN` (Not-a-Number) values differently than the standard IEEE 754 equality rules applied by the `==` operator. While `x == double.NaN` always evaluates to `false`, the pattern `x is double.NaN` evaluates to `true` if the runtime value of `x` is `NaN`.
* **Null Checking:** The `null` constant 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 to `object.ReferenceEquals(expr, null)`).

```csharp theme={"dark"}
// --- Implicit Conversions 
int x = 5;
// bool match1 = x is 5L; // Compiler error: constant of type 'long' cannot be implicitly converted to 'int'

long y = 5L;
bool match2 = y is 5;     // Compiles: constant '5' (int) is implicitly converted to 'long'

// --- Floating-Point NaN 
double value = double.NaN;

// Standard equality operator follows IEEE 754 rules
bool isNanOperator = value == double.NaN; // Evaluates to false

// Constant pattern uses object.Equals semantics
bool isNanPattern = value is double.NaN;  // Evaluates to true

// --- Null Checking 
string text = null;

// Bypasses custom == operators, equivalent to object.ReferenceEquals(text, null)
bool isNull = text is null; 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
