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

The `?:` operator, formally known as the ternary conditional operator, evaluates a boolean expression and returns the result of one of two mutually exclusive expressions based on that evaluation. It is an expression-based construct, meaning it resolves to a value, unlike the statement-based `if-else` control flow.

```csharp theme={"dark"}
bool condition = true;
int consequent = 1;
int alternative = 0;

int result = condition ? consequent : alternative;
```

## Evaluation Mechanics

The operator requires three operands:

1. **`condition`**: An expression that must be implicitly convertible to `bool`, or a type that explicitly overloads the `true` and `false` operators.
2. **`consequent`**: The expression evaluated and returned if the `condition` evaluates to `true`.
3. **`alternative`**: The expression evaluated and returned if the `condition` evaluates to `false`.

The operator enforces **short-circuit evaluation**. The compiler guarantees that only one of the two result expressions is evaluated. If `condition` is `true`, `consequent` is evaluated and `alternative` is ignored. If `condition` is `false`, `alternative` is evaluated and `consequent` is ignored.

## Type Resolution

The compiler must determine a single return type for the entire ternary expression. The rules for type resolution depend on the C# version:

* **Standard Type Resolution:** There must be an implicit conversion from the type of the `consequent` to the type of the `alternative`, or vice versa. If neither conversion exists, or if both exist (creating an ambiguity), the compiler throws an error.
* **Target-Typed Conditional Expressions (C# 9.0+):** If the `consequent` and `alternative` do not share a common type, the expression is valid provided both types are implicitly convertible to the target type of the assignment or context.

```csharp theme={"dark"}
using System.Collections.Generic;

bool condition = true;

// Standard resolution: 'int' implicitly converts to 'double'
double result = condition ? 10.5 : 5; 

// Target-typed resolution (C# 9.0+): Both convert to 'IEnumerable<int>'
IEnumerable<int> sequence = condition ? new List<int>() : new int[5];
```

## Associativity

The ternary operator is **right-associative**. When multiple conditional operators are chained, they are grouped from right to left.

```csharp theme={"dark"}
bool a = true;
bool c = false;
int b = 1, d = 2, e = 3;

// The expression:
int chainedResult = a ? b : c ? d : e;

// Is evaluated by the compiler as:
int groupedResult = a ? b : (c ? d : e);
```

## Ref Conditional Operator

Starting with C# 7.2, the ternary operator can be used to return a variable reference (`ref`) rather than a value. Both the `consequent` and `alternative` must be `ref` expressions of the exact same type. When assigning the result to a `ref` local, the entire ternary expression must be enclosed in parentheses to parse correctly.

```csharp theme={"dark"}
bool condition = true;
int variable1 = 10;
int variable2 = 20;

ref int reference = ref (condition ? ref variable1 : ref variable2);
```

## Restrictions

* The `?:` operator cannot be overloaded via user-defined operator overloading.
* Because it is an expression, it cannot be used as a standalone statement; its result must be assigned, passed as an argument, or returned.

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