Null-conditional operator

The worst thing that a developer can do is not check for null in code. This means that there is no reference to an object, in other words, there is a null. Reference-type variables have a default value of null. Value types, on the other hand, cannot be null. In C# 2, developers were introduced to the nullable type. To effectively make sure that objects are not null, developers usually write sometimes elaborate if statements to check whether objects are null or not. C# 6.0 made this process very easy with the introduction of the null-conditional operator.

It is expressed by writing ?. and is called the question-dot operator. The question is written after the instance, right before calling the property via the dot. An easy ...

Get C# Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.