Null-Conditional Operators

null can be quite a nuisance: each time you receive an object, you’re technically supposed to check for null. C# has added some operators that alleviate the syntactic burden null imposes on you.

Member Access

Consider the code below:

if ( someObject != null )
{
    someObject.someAction();
}

This can be replaced by

someObject?.someAction();

The ?. operator first checks if someObject is null, and only if that is not the case, the method someAction will be called.

In case someAction returns a value:

var result = someObject?.someAction();

result will contain null if someObject is null, otherwise it will be set to someAction’s return value. If necessary, the return type is made nullable, e.g., int is upgraded to int? in order to null values.

Indexing

C# offers a similar constructor for indexing:

if ( someContainer != null )
{
    var element = someContainer[i];
}

can be replaced by

var element = someContainer?[i];

Here, element will be assigned null if someContainer is null.

Null Coalescing Operator

Say you have an object x which you don’t want to be null. If it is, you’d like this null to be replaced by some default value:

x != null ? x : defaultValue

C# provides the null coalescing operator to shorten the syntax:

x ?? defaultValue

For example, this can come in handy to remove the “nullability” from value types:

void Foo(int? x)
{
    int y = x ?? 0; // Either use x, or 0 if x is null
}

Further Reading