Deep null checking, is there a better way?

We have considered adding a new operation “?.” to the language that has the semantics you want. (And it has been added now; see below.) That is, you’d say

cake?.frosting?.berries?.loader

and the compiler would generate all the short-circuiting checks for you.

It didn’t make the bar for C# 4. Perhaps for a hypothetical future version of the language.

Update (2014):
The ?. operator is now planned for the next Roslyn compiler release. Note that there is still some debate over the exact syntactic and semantic analysis of the operator.

Update (July 2015): Visual Studio 2015 has been released and ships with a C# compiler that supports the null-conditional operators ?. and ?[].

Leave a Comment