Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have several plain old C# objects that are returned by an API, and have several layers of POCOs nested within them. I need to access fields contained deep within these structures, but because the API leaves these nested objects as null when data is missing, I find myself having to do lots of null checks just to get at the field I really want.

if(obj != null && obj.Inner != null && obj.Inner.Innerer != null) { ... }

The shortest form I've come up with is using the ternary operator.

obj != null && obj.Inner != null && obj.Inner.Innerer != null ? obj.Inner.Innerer.Field : null;

Does C# have any way to do this without having to write out all those comparisons? I'd really like something short and simple like:

obj.Inner.Innerer.Field ?? null;

But that only checks Field for null.

share|improve this question

marked as duplicate by Martin Smith, Robert Rouhani, Yotam Omer, Mr. Alien, Ryan Jul 27 '13 at 4:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
How about this? –  rene Jul 26 '13 at 17:25
    
Hmm, the f method is definitely too messy. It makes the line of code shorter, but I'd say ternary is easier to understand. The tc method is pretty close, but throws an exception in the method which is a performance penalty (especially compared to a null check). So close, but not exactly what I'd hope for. –  Syon Jul 26 '13 at 17:39
    
An idea is that you could try to directly access Inner.Innerer.Field while trying to catch a null exception. If anything along the road is null, the exception is caught and you return null. Otherwise you return the value. –  KappaG3 Jul 26 '13 at 18:00
    
No, exceptions won't be a "performance penalty" on any modern computers. It's for "exceptional behaviors", you won't loop on the exception million of times. –  Pierre-Luc Pineault Jul 26 '13 at 18:11
1  
@KappaG3 That is only possible if the getter is just a return of such backing field but there are implementation out in the wild that do a lot of work in the get method. –  rene Jul 26 '13 at 18:14

2 Answers 2

It's not beautiful, but you could write accessor functions in your code that would encapsulate the null checks so you only have to do it in one place. E.g.

Innerer GetInnerer()
{
  if(obj != null && obj.Inner != null && obj.Inner.Innerer != null)
  {
    return obj.Inner.Innerer;
  }

  return null;
}

That way you could call it like this: if (myObject.GetInnerer() != null).... You would have to create a number of these functions, but at least it would move the 'goreyness' of null checking to a single location.

share|improve this answer

Please have a look at this very clever and elegant solution:

http://www.codeproject.com/Articles/109026/Chained-null-checks-and-the-Maybe-monad

share|improve this answer
    
Interesting, but to make this chaining work I would need to modify the objects return from the API, and I have no control over them. –  Syon Jul 26 '13 at 19:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.