Brendan Enrick

Daily Software Development

Just In Time Properties

Properties with backing fields can easily be null. I often see properties which check the backing field to see if it is null. This is commonly done with an if statement with one line of initialization. One way to get around this is to use the coalescing operator in C#. If we use this in combination with an expressions, which might be a method, we are able to easily handle this property with one line.

Here is an example of what I am talking about.

private SomeType _someObject;
public SomeType SomeObject 
{ 
    get 
    { 
        return _someObject ?? (_someObject = giveMeSomeObject()); 
    } 
}

Notice how short and concise this is. I am able to with one line which is very clear handle this common scenario. Sure it doesn't always match this exactly, but I see plenty of properties people write where they do something similar.

I think that is very elegant and a lot nicer than doing this older style.

private SomeType _someObject;
public SomeType SomeObject 
{ 
    get 
    { 
        if (_someObject == null)
        {
            _someObject = giveMeSomeObject(); 
        }
        return _someObject;
    } 
}

Comments (4) -

  • 3/9/2009 4:10:11 AM | Reply

    It seems that the old style is faster. Look what reflector shows for the new style:


    .method public hidebysig specialname instance object get_SomeObject() cil managed


    {


        .maxstack 3


        .locals init (


            [0] object obj2)


        L_0000: ldarg.0


        L_0001: ldfld object Snippet::_someObject


        L_0006: dup


        L_0007: brtrue.s L_0018


        L_0009: pop


        L_000a: ldarg.0


        L_000b: call object Snippet::giveMeSomeObject()


        L_0010: dup


        L_0011: stloc.0


        L_0012: stfld object Snippet::_someObject


        L_0017: ldloc.0


        L_0018: ret


    }


    and for the old style:


    .method public hidebysig specialname instance object get_SomeObject2() cil managed


    {


        .maxstack 8


        L_0000: ldarg.0


        L_0001: ldfld object Snippet::_someObject2


        L_0006: brtrue.s L_0013


        L_0008: ldarg.0


        L_0009: call object Snippet::giveMeSomeObject()


        L_000e: stfld object Snippet::_someObject2


        L_0013: ldarg.0


        L_0014: ldfld object Snippet::_someObject2


        L_0019: ret


    }


  • Brendan Enrick

    3/9/2009 3:17:17 PM | Reply

    Thanks for the info. Glad to know people are checking the stuff I post.


    That brings up a very interesting point. What is more important when developing a certain application? There are plenty of goals you may have; conciseness, clarity, performance, etc. These goals may differ between applications, so it is very important to consider this question when deciding how code should be written.


  • 3/9/2009 9:46:11 PM | Reply

    You're welcome. Link to this post was in my RSS "CSharp Feeds" Smile


    Anyway. For me the performance is important in about 80%. There are although some situations where there is no sense to maintain highly fast (=unsafe or unmaintainable) code.


    But I would definitely use ?? operator instead of the old style if the SomeStyle is class or relatively small struct (so "dup" opcode won't be slow with copying that)... anyway I wonder why they used "dup" instead of generating something similar to the old style.


  • 3/9/2009 9:47:40 PM | Reply

    I wrote "SomeStyle" instead of "SomeType"


    That is 5 x typo Tong


Loading