Stats

1236 Contributors: 31 Tuesday, March 28, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap
Download eBook

Null-Coalescing Operator

Download c# eBook

Syntax

  • var result = possibleNullObject ?? defaultValue;

Parameters

ParameterDetails
possibleNullObjectThe value to test for null value. If non null, this value is returned. Must be a nullable type.
defaultValueThe value returned if possibleNullObject is null. Must be the same type as possibleNullObject.

Remarks

The null coalescing operator itself is two consecutive question mark characters: ??

It is a shorthand for the conditional expression:

possibleNullObject != null ? possibleNullObject : defaultValue

The left-side operand (object being tested) must be a nullable value type or reference type, or a compile error will occur.

The ?? operator works for both reference types and value types.

Related Examples