Beware of Static Constructors

By | February 25, 2009

In Bunian we needed to use a static constructor for some reason, it was all going good; we tested the code and it ran smoothly…excellent (Code Coverage anyone?!).
But when I came across this situation, it appeared that the static constructor wasn’t invoked!even when “Class.Method();” is called! so lets examine it.

I have two simple classes as an Example:

public class Parent

{

    public static string DoSomething()

    {

        return “Parent: DoSomething() called”;

    }

}

public class Child : Parent

{

    static Child()

    {

        Console.WriteLine(“Child Static constructor called”);

    }

 

    public static string DoSomethingDifferent()

    {

        return “DoSomethingDifferent() called”;

    }

}

As it may be obvious, Child inherits from Parent, Child has a static constructor that we need to be executed when ever a method is invoked by Child. Now lets check the main program executing these two lines:

Console.WriteLine(Child.DoSomething()); //This code will NOT invoke the static constructor

Console.WriteLine(Child.DoSomethingDifferent());//This code WILL invoke the static constructor

The surprise (at least to me) when calling “Child.DoSomething()” the static constructor isn’t invoked! because it is in the parent!! aaaaaah! bad!! that was serious for our architecture  and we had to do lots of fixes to turns things around the right way (which I think it was for our own good for other reasons :P)

This brings up the Code Coverage topic as well; in our case that static constructor’s job was to create an instance of a member that is only needed once, and we check on it in other times by “if _instance != null”. It always ran ok because all the test code we created used to call an original Child before calling any other method that resided in the Parent.

bottom line: be ware of static constructors, and check your test code…it maybe hiding “surprises” for you πŸ˜‰

4 thoughts on “Beware of Static Constructors

  1. LUNAR

    Salam
    I came through this once, i needed a hashtable that must be initialized once the class is referenced (only once), so , a superior at my company suggested to use a static constructor, it worked fine with me because i dont need to update this hashtable with a different data each time, initialized once and manipulated as much as i want in other portions of the code.

    Great Tip (thumbs UP).

  2. admin Post author

    Thanks a lot for passing by Lunar, and it’s good that you didn’t fall in the trap i am talking about hehe. teslam πŸ™‚

  3. Ibrahim Younis

    regardless of the needed time to complete this project even if it completed far away form perfection but still you did you best, as they say later better than never, but make to come after all of the waiting.

    Gazakm allah Khayran, hope that Allah calculate all of your efforts in you MEZAN 7ASANTKOM.

    your brother

  4. Emad Alashi Post author

    πŸ™ someday it’s gonna see the light enshallah, if only we can buy time! but i admit, we shoud put more effort in this. thanks ibrahim πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *