Null propagating operator in C#


How null propagating operator works in C#

Null propagating operator (?) is in C# to make it easier to handle null values in property or method call chain and stop it as soon as first null value is found.

Consider the following example where first child of John Doe is written out to console. Take a good look on this code and think if this is good code.

The code above is ugly. It's possible to hide it to some property or method of Person class but it doesn't solve the problem as long as the code looks like it is.

Null propagation operator helps us to make this code readable.

As soon as something before ?. is null the call chain is stopped and null is returned. The line that writes out name of first child first gets name of first child and if it is null then "-" is returned instead.


Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to