C# Reference ยท Operators and Expressions
C# null-conditional operators ?. and ?[]
Reference for the C# null-conditional operators ?. and ?[]: safe member and index access, short-circuiting chains, delegate invocation and assignment.
The null-conditional operators ?. and ?[] access a member or an element only if the object on the left is not null. If it is null, the whole access is skipped and the result is null instead of a NullReferenceException.
The result type is nullable: person?.Age is an int? when Age is an int. Combine the operator with ?? to turn the missing case into a default value.
int? length = text?.Length; string name = order?.Customer?.Name ?? "unknown";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
result = target?.Member; result = target?[index]; target?.Method(args); target?.Property = value; // C# 14
| Form | Description |
|---|---|
a?.b |
Member access: evaluates to a.b if a is not null, else null. |
a?[i] |
Index access: evaluates to a[i] if a is not null, else null. |
a?.M(x) |
Method call; arguments are not evaluated when a is null. |
d?.Invoke(x) |
Safe delegate or event invocation. |
a?.b = v |
Assignment (C# 14); v is evaluated only if a is not null. Compound forms like += work too. |
Returns: the member's value converted to a nullable type (T?), or null. A method returning void can be called, but the call is then a statement and not an expression.
Errors: a?.b cannot be used where a non-nullable result is required without ??, ! or a null check. ?. cannot be applied to a pointer.
Usage notes
- Whole chains short-circuit. In
a?.B.C.D(), ifaisnullthe rest of the chain is not evaluated at all. A single?.at the start is enough when the later members cannot benull; add?.at each step that can. - Always lifted to nullable.
list?.Countisint?, soif (list?.Count > 0)works (the comparison withnullis false), butint n = list?.Count;does not compile. - Thread-safe delegate invocation.
handler?.Invoke(...)reads the delegate once, so another thread unsubscribing cannot cause aNullReferenceExceptionbetween the check and the call. This is why it replacedif (handler != null) handler(...). - Do not overuse it.
?.on a value that can never benullhides bugs. Ifnullis a real error, throw early instead of skipping silently. - Not available on a left operand that is a non-nullable value type;
5?.ToString()is a compile error. - Nullable reference types. The compiler tracks the state: after
if (s?.Length > 0), it knowssis not null inside the block. - Expression trees do not support
?.. - Performance: one null check and branch.