> ## 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# If Statement

The `if` statement is a fundamental control flow construct in C# that dictates the execution path of a program based on the evaluation of a boolean expression. It branches execution by evaluating a condition and executing the associated statement block strictly if the condition resolves to `true`.

```csharp theme={"dark"}
if (booleanExpression)
{
    // Statements executed if booleanExpression is true
}
else if (secondaryBooleanExpression)
{
    // Statements executed if the previous conditions are false 
    // and secondaryBooleanExpression is true
}
else
{
    // Statements executed if all preceding conditions are false
}
```

## Technical Mechanics

**Boolean Evaluation**
Unlike C or C++, the C# compiler enforces strict type checking on the condition. The expression evaluated by the `if` statement must resolve to a `bool`, a custom type that provides an implicit conversion operator to `bool`, or a type that explicitly overloads the `true` and `false` operators. Implicit conversions from standard integers (e.g., `if (1)`) or object references (e.g., `if (ptr)`) are not permitted and will result in a compilation error (CS0029).

**Block Scoping and Memory**
Variables declared within the curly braces `{ }` of an `if`, `else if`, or `else` branch are lexically scoped to that specific block. Once execution exits the block, the lexical scope ends, making the variables inaccessible to the compiler.

However, exiting the block does not deallocate stack memory. Local variables within a block—whether they are value types or references (pointers) to reference types—share the enclosing method's stack frame, which is allocated by the JIT compiler upon method entry. The memory for these variables is only reclaimed when the method returns and the stack frame is popped. The compiler may, however, optimize memory by reusing the block variable's stack slot for other variables declared later in the same method. For reference types, the Garbage Collector is responsible for reclaiming the heap-allocated *object* the variable points to once it is no longer reachable, not the variable reference itself.

**Brace Omission and the Dangling Else**
When a branch contains only a single statement, the curly braces can be omitted.

```csharp theme={"dark"}
if (condition)
    SingleStatement();
else
    AlternativeStatement();
```

While syntactically valid, omitting braces can lead to the "dangling else" problem, where an `else` clause binds to the nearest preceding `if` statement within its scope, potentially contradicting the developer's indentation and intent.

**Pattern Matching Integration**
Modern C# extends the `if` statement to support pattern matching directly within the boolean expression. This allows for simultaneous type checking, casting, and variable declaration.

```csharp theme={"dark"}
object obj = "Technical Documentation";

if (obj is string stringValue)
{
    // stringValue is definitely assigned and strongly typed as a string here
    int length = stringValue.Length;
}
```

Variables declared via pattern matching inside the `if` condition "leak" into the enclosing scope, but their accessibility is strictly governed by the compiler's definite assignment rules. Definite assignment depends on the specific logical flow guaranteeing the pattern matched. For example, in `if (!(obj is string s))`, `s` is definitely assigned and safely accessible in the `else` block (where the condition evaluated to `false`). Conversely, a condition like `if (obj is string s || true)` prevents `s` from being definitely assigned anywhere, rendering it inaccessible despite the overall condition evaluating to `true`.

**Short-Circuit Evaluation**
When the boolean expression contains conditional logical AND (`&&`) or conditional logical OR (`||`) operators, the expression will undergo short-circuit evaluation. This behavior is a built-in semantic property of the conditional logical operators themselves, not a feature or behavior of the `if` statement. The operators will bypass the evaluation of subsequent operands if the overall truth value of the expression can be definitively determined by the preceding operands. The `if` statement then simply branches based on the final boolean result yielded by the expression.

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