I think the C# language could really use a new operator for this sort of logic - it's quite common, and an operator would simplify countless lines of code out there.
We need something along the lines of the "??" or "if null then" operator, but that works as special '.' dot operator with a "if not null" condition. For "??", the first '?' is like the 'if' part of the "?:" if-then, and the second '?' represents 'null' like for nullable types. I think we need a ".?" operator which works just like '.', except it simply evaluates to null if the left expression is null instead of throwing an exception. I guess it would be a "dot not null" operator (OK, so maybe ".!?" would be more logical, but let's not go there).
So, your oh-so-common example could lose the redundant symbol name like this:
var a = long_expression.?Method();
There is a ton of C# code out there with nested null checks that would benefit greatly. Just think how nice it would if this:
if (localObject != null)
{
if (localObject.ChildObject != null)
{
if (localObject.ChildObject.ChildObject != null)
localObject.ChildObject.ChildObject.DoSomething();
}
}
could become just:
localObject.?ChildObject.?ChildObject.?DoSomething();
There is also a ton of code where null checks that should be there are missing, resulting in occasional runtime errors. So, actually using the new '.?' operator by default would eliminate such problems... It's perhaps unfortunate that we couldn't reverse the behavior of '.' and '.?' at this point, so only the new '.?' throws an exception if the left side is null - it would be the cleaner and more logical way to go, but breaking changes are very bad. Although, one could argue that this particular change would be likely to fix a lot of hidden problems, and unlikely to break anything (only code that expects a null ref exception to be thrown). Oh, well... one can always dream...
The only downside to checking for the null is really the slight performance hit, which is most assuredly why '.' doesn't check for null. But, I really think the ability to just use '.?' when you care about performance (and know the left side will never be null) would have been the better way to go.
On a similar note, having 'foreach' check for and ignore a null collection would have also been so much nicer. No more need to wrap most 'foreach' statements with "if (collection != null)", or worse, develop the common habit of always using empty collections instead of null ... which is fine in some cases, but an even worse performance issue than the null check when this is done in complex object trees where the majority of the collections are empty (which I've seen a lot). I think the good intention of not having 'foreach' check for null to provide better performance has backfired in the majority of cases.
Anders, it's never too late to make such changes, and add a compiler switch to enable them!