Multiple functions to establish a fallback value for a property until not null I have a property (called "Head") that can be set externally. When "Head" is not set, it uses Unity3D's MonoBehaviour functions to try to find the best value for "Head". I started with a nested if, but the nesting was starting to get out of hand.

if (Head == null)
{
    Head = this.GetComponent&lt;Camera&gt;();

    if (Head == null)
    {
        Head = this.GetComponentInChildren&lt;Camera&gt;();

        if (Head == null)
        {
            Head = Camera.main;
        }
    }
}


So I developed a loop:

System.Func&lt;Camera&gt;[] fallbacks = new System.Func&lt;Camera&gt;[]
{
    () =&gt; this.GetComponent&lt;Camera&gt;(),
    () =&gt; this.GetComponentInChildren&lt;Camera&gt;(),
    () =&gt; Camera.main,
};

foreach (var fallback in fallbacks)
{
    if (Head != null)
    {
        break;
    }
    Head = fallback();
}


Would this create too much lambda overhead? Do you think the lambdas even increases readability? Is there a more elegant way to handle multiple fallbacks?

Edit:

RobH helped me discover the Null coalescing doesn't work with Unity3D components.
RobH's extension method workaround worked great with some tweaking. I had to enforce that comparison had to be done through UnityEngine.Object.

public static T IfNullThen&lt;T&gt;(this T obj, Func&lt;T&gt; factory) where T : UnityEngine.Object
{
    return obj != null ? obj : factory();
}


With that extension method my code is now just

Head = Head
    .IfNullThen(() =&gt; this.GetComponent&lt;Camera&gt;())
    .IfNullThen(() =&gt; this.GetComponentInChildren&lt;Camera&gt;())
    .IfNullThen(() =&gt; Camera.main);
This seems to be a textbook case for the null coalescing operator ??

Head = GetComponent&lt;Camera&gt;() ?? GetComponentInChildren&lt;Camera&gt;() ?? Camera.main;


You also (probably) don't need to prefix with this

Edit:

If the null coalescing operator doesn't work (which is a massive shame) why not create an extension method?

public static T IfNullThen&lt;T&gt;(this T obj, Func&lt;T&gt; factory) where T : class
{
    return obj != null ? obj : factory();
    // or
    // return obj == null ? factory() : obj;
}


then you can do:

Head = GetComponent&lt;Camera&gt;().IfNullThen(GetComponentInChildren&lt;Camera&gt;).IfNullThen(() =&gt; Camera.main);


Unfortunately I'm on my mac so I don't have a compiler but I think it should work :)