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

The `?.` operator, formally known as the null-conditional member access operator, performs a member access or method invocation on its left-hand operand only if that operand evaluates to a non-null value. If the left-hand operand evaluates to `null`, the operator short-circuits the expression and immediately returns `null`.

```csharp theme={"dark"}
// General syntax
var result = operand?.Member;
```

## Evaluation Semantics

When the compiler processes a null-conditional operation, it generates Intermediate Language (IL) that evaluates the left-hand operand exactly once.

Logically, `A?.B` is similar to the ternary expression `(A != null) ? A.B : null`. However, the `?.` operator caches the result of `A`. This single-evaluation guarantee ensures thread safety, preventing race conditions where a reference might be modified by another thread between a manual null check and the subsequent member access.

## Type Promotion

The return type of a null-conditional expression is determined by the underlying type of the accessed member:

1. **Reference Types:** If the accessed member is a reference type, the return type of the expression remains that reference type.
2. **Value Types:** If the accessed member is a non-nullable value type `T` (such as `int`, `bool`, or `struct`), the compiler automatically promotes the return type to the corresponding nullable value type `Nullable<T>` (expressed in C# as `T?`).

```csharp theme={"dark"}
string text = null;

// String.Length is of type 'int'. 
// The ?. operator promotes the result to 'int?'.
int? length = text?.Length; 
```

## Short-Circuiting in Expression Chains

The `?.` operator exhibits short-circuiting behavior across chained member accesses. If the left-hand operand evaluates to `null`, the remainder of the expression chain is bypassed entirely.

```csharp theme={"dark"}
// If 'A' is null, neither 'B' nor 'C' are evaluated.
// If 'A' is not null, but 'B' is null, 'C' is not evaluated.
var result = A?.B?.C;

// Short-circuiting applies to standard member access (.) following a null-conditional access.
// If 'A' is null, the standard access to 'C' is safely bypassed.
var mixedResult = A?.B.C; 
```

## Method Invocation

When applied to method calls, the `?.` operator prevents the invocation of the method if the target instance is `null`. The arguments passed to the method are also not evaluated if the short-circuit occurs.

```csharp theme={"dark"}
// If 'obj' is null, 'ComputeValue()' is never executed, 
// and 'result' is assigned null.
var result = obj?.ComputeValue();
```

## Limitations

* **Void-returning methods:** The `?.` operator can be used to invoke methods that return `void`. In this context, the expression does not return a value and cannot be assigned to a variable; it simply acts as a safe invocation statement.
* **Delegates:** When invoking delegates, the `?.` operator cannot be used with standard invocation syntax `delegate?.()`. It must be combined with the explicit `Invoke` method: `delegate?.Invoke()`.
* **Out parameters:** The `?.` operator cannot be used on a method invocation that includes `out` parameters, as the compiler cannot guarantee the `out` parameter will be assigned a value if the expression short-circuits.

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