Possible Duplicate:
Deep Null checking, is there a better way?
for example, if you are performing a logic on Foo1.Bar1.Foo2.Bar2 (and each of the properties can be null), you can't just do that to foo.Bar1.Foo2.Bar2 because it is possible that you get null reference exception
Currently this is what I do
if (foo1!=null && foo1.Bar1!=null && foo1.Bar1.Foo2 !=null && foo1.Bar1.Foo2.Bar2!=null)
return DoStuff(foo1.Bar1.Foo2.Bar2); //actually a logic based on the value of Bar2
else return null;
Is there a more elegant or convenient way, to do this?