Andrew Matthews

Continue if not null operator? Yes please!

Advertisements

Olmo made a very worthwhile suggestion on the LINQ forums recently. His suggestion was for a new operator to be added to the C# language to allow us to do away with the following kind of pesky construct:


 
string x;
if(a != null && a.Address != null && a.Address.FirstLine != null)
    x = a.Address.FirstLine;

instead he suggested a new ?. operator so that we could produce something like this:


 
x = a?.Address?.FirstLine;

and at the first sign of failure it would just yield a null or the equivalent. I like this suggestion, it is elegant, and provides a similar meaning to the coalescing operator. The C# team have been adding lots of these little bits of syntactic sugar recently, so why not add another that will save us lots of thrown exceptions or grisly if statements?

Advertisements

Advertisements