Skip to main content

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.

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.
// 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?).
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.
// 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.
// 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.
Master C# with Deep Grasping Methodology!Learn More