Static constructors: populating static collections


Static Constructors

As Wikipedia explains, a constructor is a special method that is used to initialize member data. This member data can be object’s data, i.e. for each individually object (instance of a class) these fields are initialized with specific values, or class’s data known as static data, available to all instance objects as 1 global resource.

We all know object constructors, but are there any other constructors? Well, YES: static constructors!

Static constructors are used like “normal” constructors, but are prefixed with the keyword “static” and are used to initialize static members. This initialization occurs when the class is used for the first time.

Here is a simple example:

 

using System;

using System.Collections.Generic;

using System.Text;

namespace StaticConstructorExample

{

class SimpleStaticExample

{

public static int staticValue;

public int instanceValue;

static SimpleStaticExample()

{

staticValue = 15;

}

public SimpleStaticExample(int param)

{

this.instanceValue = param – staticValue;

}

}

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Preparing to create some SimpleStaticExample objects”);

SimpleStaticExample object1 = new SimpleStaticExample(100);

SimpleStaticExample object2 = new SimpleStaticExample(200);

SimpleStaticExample object3 = new SimpleStaticExample(300);

Console.WriteLine(“Objects created”);

Console.ReadLine();

}

}

}

In this class there is 1 static member – staticValue -, an instance member – instanceValue – , and 2 constructors: an instance and a static constructor. Basically what the static constructor does is to initialize the static value, based on the code inside it. Here it initializes the staticValue field to 15.

Using the debugger with a breakpoint on the line with the staticValue declaration and another breakpoint in the Main() method (inside a different class), walking line by line with F10, we can see exactly how this works:

  • object1 is attempted to be created in main() (this is the first time class StaticConstructorExample is used)
  • the static constructor is invoked, initializing staticValue
  • the normal constructor is invoked and object1 is created
  • object2 is attempted to be created in Main()
  • the normal constructor is invoked and object2 is created
  • object3 is attempted to be created in Main()
  • the normal constructor is invoked and object3 is created

Debugger print-screens:



So basically what I did so far is obtain a functionality equivalent to:

public static int staticValue = 15;

Not that impressive! You may probably ask why someone would use static constructors rather than normal initialization, especially if I tell you that it has some performance penalties.

Populating static collections

The fact is that some types cannot be initialized, or better yet, populated using constructors. This would be the case of classes from the System.Collections.Generic namespace. Just try to use pre-populated Dictionary<K,V> or a Queue<T>.

So what static constructor is useful for is to initialize static fields that cannot be “fully” initialized just by using new.

Here is an example of pre-populating a static Dictionary:

 

using System;

using System.Collections.Generic;

using System.Text;

namespace StaticConstructorExample

{

class CollectionStaticExample

{

static Dictionary<int, string> dict = new Dictionary<int,string>();

int field1;

static CollectionStaticExample()

{

dict.Add(1, “1st element”);

dict.Add(2, “2nd element”);

dict.Add(3, “3rd element”);

}

public CollectionStaticExample(int i)

{

this.field1 = i;

}

}

class Program

{

static void Main(string[] args)

{

CollectionStaticExample p1 = new CollectionStaticExample(1);

CollectionStaticExample p2 = new CollectionStaticExample(2);

CollectionStaticExample p3 = new CollectionStaticExample(3);

}

}

}

Resources

MSDN Library gives static constructors‘ properties, in a programming guideline:

  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

And here are some additional articles:

  1. #1 by Cornel on August 18, 2007 - 15:52

    Static constructors can also be used to initialize static classes. More on this subject on MSDN: http://msdn2.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

    Nice article by the way :)

  2. #2 by Tudor T. on August 18, 2007 - 20:45

    Interesting article!

    Anyway, it would be useful to give some examples of when static constructors and static members in classes are useful in OOP programming (because static properties and methods in a class can be easily misused as global variables or global functions).

  3. #3 by Tudor Vlad on August 19, 2007 - 19:38

    Thanks for the feedback, guys.
    I originally wanted to restrict my article to static collection population , using static constructors. Static constructors were the means to do that.
    Although I knew they existed, I never used them before. I think they aren’t that common, that’s why I detailed them.

  4. #4 by Cornel on August 20, 2007 - 18:39

    In combination with a static class, I would use them for handling the eternal singleton design pattern :) .

  5. #5 by Tudor Vlad on August 20, 2007 - 19:59

    That’s true! It’s very elegant.

  6. #6 by raj n pandya on July 4, 2008 - 13:23

    my name is raj
    I surf this site so I very glad to load it

  7. #7 by amber necklaces on February 1, 2012 - 6:22

    Hello, you used to write fantastic, but the last few posts have been kinda boring… I miss your tremendous writings. Past few posts are just a little out of track! come on!

(will not be published)