Hugoware

The product of a web developer with a little too much caffeine

Archive for March 28th, 2010

Static Constructors — What? What???

with 3 comments

The best days of the year fall on the days that I learn something so completely new and useful in C# that I literally rush out to find a good place to use it. It has happened several times over the years, for example when discovered coalescing operators and ternary operators (not limited to C# but you get it).

Well, that great day has arrived again!

So look at this code – I write code like this fairly often…

/// <summary>
/// Manages resources or something...
/// </summary>
public class WebResources {

    /// <summary>
    /// Returns a list of resource files
    /// </summary>
    public static IEnumerable<Resource> ResourceList {
        get {

            //check for the resource
            if (WebResources._ResourceList == null) {
                WebResources._ResourceList = WebResources._LoadResources();
            }

            //return the resources
            return WebResources._ResourceList;

        }
    }
    private static IEnumerable<Resource> _ResourceList;

    //snip...
}

The goal of the code was to create a static property that only allowed for you to get the values. I’d always end up having to write extra code to check for the value then load it if it wasn’t ready yet. This threw an extra if statement into every request.

While a single extra comparison isn’t really going to be a big deal I still felt the code hurt the clarity of the class. Properties are rarely supposed to do anything. Talking to external resources and loading files… not really a good idea. Not only that, it placed setup code in the middle of the class instead up mixed in with the constructors where it really belongs.

However, behold coding greatness…

/// <summary>
/// Manages resources or something...
/// </summary>
public class WebResources {

    //Oh yeah!!
    static WebResources() {
        WebResources.ResourceList = WebResources._LoadResources();
    }

    /// <summary>
    /// Returns a list of resource files
    /// </summary>
    public static IEnumerable<string> ResourceList { get; private set; }

    //snip...
}

You see that? A static constructor! It feels like I’ve discovered something rare like a unicorn or something… but in the form of a code snippet.

Now you should note that this code doesn’t run as soon as your application starts. Instead, it is fired off the first time you access the class. It is important to keep that in mind – Imagine if you have a long running process and it kicks off in the middle of your programming running. Here are a few examples of what I mean…

Examples where the static constructor will NOT run


//checking the type against another object
someObject is WebResources

//loading the Type data for the class
typeof(WebResources)

//trying to cast the value
WebResources temp = someObject as WebResources;

Examples where the static constructor WILL run


//creating an instance
WebResources resources = new WebResources();

//accessing a property
IEnumerable<Resource> resources = WebResources.Resources;

//using an inheriting class
public class SuperWebResources : WebResources { }
IEnumerable<Resource> resources = SuperWebResources.Resources;

Its too bad I’m only just now finding out about this little gem of code. I’m sure some of you smart devs could have clued me in about this a long time ago. I guess that’s just the way it goes being the only programmer in your company for this long… Oh well… 😐

Anyways – Helpful? Known it all along (And if so, why have you been holding out on me!)

Advertisements

Written by hugoware

March 28, 2010 at 9:18 pm

Posted in General Discussions

Tagged with , , ,