At work, I have many cases, when in deep object hierarchy I need to get value from some property:

var name = characteristics.Materials.MainMaterial.Name;

Problem is, that in many cases I'm not sure, that higher level objects like Materials or MainMaterial in given example have value, so I need to check that to avoid NullReferenceException:

var name = characteristics.Materials == null ? 
                           null : 
                           (characteristics.Materials.MainMaterial == null ? 
                                            null : 
                                            characteristics.Materials.MainMaterial.Name);

That would be quite nasty line, or if I would convert it to if sentences, it still would be ugly. So for such cases we have following extension method:

public static TResult Access<TSource, TResult>(
                        this TSource obj, 
                        Func<TSource, TResult> expression, 
                        TResult defaultValue = default(TResult))
{
     if (obj == null)
     {
         return defaultValue;
     }

     return expression.Invoke(obj);
}

Using this extension, we can rewrite our name access like this:

var name = characteristics.Materials.Access(x => x.MainMaterial.Access(y => y.name));
c#