> ## 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# Else If Clause

The `else if` clause in C# is a conditional branching construct that evaluates a boolean expression only when all preceding `if` and `else if` conditions within the same control structure evaluate to `false`. Technically, the C# language specification does not define a distinct `else if` keyword or construct. An `else if` chain is structurally an `else` clause where the single embedded statement is another `if` statement. Standard formatting conventions simply align them to appear as a unified, sequential control structure.

## Syntax Structure

```csharp theme={"dark"}
if (condition1)
{
    // Statement(s) executed if condition1 is true
}
else if (condition2)
{
    // Statement(s) executed if condition1 is false AND condition2 is true
}
else if (condition3)
{
    // Statement(s) executed if condition1 and condition2 are false AND condition3 is true
}
```

## Execution Mechanics

* **Sequential Evaluation:** The runtime evaluates `else if` conditions strictly in a top-to-bottom order.
* **Mutual Exclusivity:** The control structure guarantees that a maximum of one branch will execute. Once an `else if` condition evaluates to `true`, its associated statement or block executes, and the runtime immediately exits the entire `if` control structure. Subsequent `else if` conditions are bypassed and remain unevaluated.
* **Boolean Evaluation Requirement:** The expression evaluated by the `else if` clause must resolve to a boolean context. While C# does not permit implicit conversions from standard numeric types or object references to booleans (unlike C/C++), the condition is valid if it evaluates to a `bool`, is a type with a user-defined implicit conversion to `bool`, is a type that overloads `operator true` and `operator false`, or is the `dynamic` type.

## Structural Rules

* **Dependency:** An `else if` clause cannot exist in isolation. It must immediately follow an `if` statement or another `else if` statement. This applies regardless of whether the preceding statement is a block enclosed in braces `{ }` or a single embedded statement without braces.
* **Multiplicity:** A single `if` control structure can contain zero or an unlimited number of chained `else if` clauses (subject only to compiler limits on syntax tree depth).
* **Terminal Clause:** An `else if` chain can optionally be terminated with a single `else` clause, which acts as the default execution path if all preceding `if` and `else if` conditions evaluate to `false`.
* **Block Scoping:** Variables declared within the body block of an `else if` clause are scoped locally to that specific block and are inaccessible to subsequent `else if` or `else` branches.

## Pattern Matching Integration and Scoping

In modern C# (C# 7.0 and later), the `else if` clause fully supports pattern matching, allowing for simultaneous type checking and variable declaration within the condition itself.

However, variable scoping rules for pattern variables declared in an `if` condition differ from standard block scoping. Variables introduced via pattern matching leak into the *enclosing block* of the entire `if` statement. Consequently, a variable declared in a preceding `if` condition remains in scope within subsequent `else if` blocks, although it is not *definitely assigned*.

```csharp theme={"dark"}
if (obj is string s)
{
    // 's' is in scope and definitely assigned
}
else if (obj is int i)
{
    // 'i' is in scope and definitely assigned
    
    // 's' is still in scope here, but is NOT definitely assigned.
    // It is legal to assign a new value to 's' within this block.
    s = "new value"; 
}
```

<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>
