Skip to content

Null-conditional operators C# 6.0code reduction

Safely access members on potentially null objects.

Checking if variables are null before they are used is a very common requirement.

The ?. and ?[] operators allow you to access methods, properties or indexers without having to worry if the variable you are using is null. If it is then the result will just be null as well.

Code

C#
string b = a?.ToString();
C#
string b = null;
if (a != null)
    b = a.ToString();

Notes

More information