> ## 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# Declaration Pattern

The declaration pattern in C# is a pattern matching construct that checks whether a runtime value is compatible with a specified type and, upon a successful match, binds the converted result to a newly declared local variable. It combines type testing and safe casting into a single operation.

## Syntax

The syntax of the declaration pattern itself consists strictly of a type and a designation (a variable name or a discard):

```csharp theme={"dark"}
Type variableName
```

* **`Type`**: The target type to match against.
* **`variableName`**: The identifier for the newly declared pattern variable. A discard (`_`) can be used if the bound value is not needed.

## Contexts of Use

Because patterns do not evaluate themselves, the declaration pattern must be hosted within a pattern-matching construct. The primary contexts include:

1. **The `is` Operator**: Evaluates the pattern and yields a boolean result.
2. **`switch` Statements**: Evaluates the pattern to determine branch execution.
3. **`switch` Expressions**: Evaluates the pattern to return a corresponding expression arm.

```csharp theme={"dark"}
object data = "Runtime String";

// Context 1: 'is' operator expression
if (data is string stringData) 
{ 
    Console.WriteLine(stringData); 
}

// Context 2: 'switch' statement
switch (data)
{
    case int intData:
        break;
}

// Context 3: 'switch' expression
var result = data switch
{
    DateTime dateData => dateData.ToString(),
    _ => "Unknown"
};
```

## Execution Mechanics

When an enclosing construct evaluates an input value against a declaration pattern, the runtime performs the following sequence:

1. **Type Testing**: The runtime checks if the input value is of type `Type` (or derived from `Type`).
2. **Null Checking**: If the input value is `null`, the pattern match fails, even if `Type` is a reference type or a nullable value type.
3. **Binding**: If the type test succeeds, the runtime casts the value to `Type` and assigns it to `variableName`.
4. **Construct Resolution**: The pattern reports match success or failure to the enclosing construct. The construct then dictates the control flow (e.g., the `is` operator yields `true`, or a `switch` routes to a specific `case`). The pattern itself does not yield a boolean value.

## Scope and Definite Assignment

The declaration pattern introduces a pattern variable into the enclosing lexical scope. However, C# enforces **flow-dependent definite assignment** rules. The variable is only accessible and considered initialized in code paths where the compiler can guarantee the pattern match succeeded.

```csharp theme={"dark"}
object data = "Runtime String";

if (data is string stringData)
{
    // stringData is definitely assigned and accessible here.
    Console.WriteLine(stringData.Length);
}

// stringData is technically in scope here, but accessing it results in 
// compiler error CS0165 (Use of unassigned local variable) because 
// it is not definitely assigned in this execution path.
```

If the pattern match dictates an early exit (such as through logical negation), the definite assignment flows to the alternative branch based on compiler flow analysis:

```csharp theme={"dark"}
if (!(data is string stringData))
{
    // stringData is unassigned here.
    return;
}

// stringData is definitely assigned here due to the early exit above.
Console.WriteLine(stringData.Length);
```

## Type Handling Characteristics

* **Reference Types**: Performs standard polymorphic type checking. The match succeeds if the input's runtime type is exactly the target type or derives from it.
* **Value Types and Unboxing**: The pattern handles unboxing automatically. If the input is a boxed `int` (e.g., stored in an `object` reference), evaluating it against the pattern `int i` will successfully unbox the value, allocate `i` on the stack, and assign the unboxed value to it.
* **Nullable Value Types**: Matching a nullable value type expression against its underlying non-nullable type (e.g., evaluating an `int?` input against the pattern `int i`) successfully extracts the underlying value, provided the input is not 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>
