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:
public static int staticValue;
public int instanceValue;
staticValue = 15;
public SimpleStaticExample(int param)
this.instanceValue = param – staticValue;
Console.WriteLine(“Preparing to create some SimpleStaticExample objects”);
SimpleStaticExample object1 = new SimpleStaticExample(100);
SimpleStaticExample object2 = new SimpleStaticExample(200);
SimpleStaticExample object3 = new SimpleStaticExample(300);
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
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:
static Dictionary<int, string> dict = new Dictionary<int,string>();
dict.Add(1, “1st element”);
dict.Add(2, “2nd element”);
dict.Add(3, “3rd element”);
public CollectionStaticExample(int i)
this.field1 = i;
CollectionStaticExample p1 = new CollectionStaticExample(1);
CollectionStaticExample p2 = new CollectionStaticExample(2);
CollectionStaticExample p3 = new CollectionStaticExample(3);
- 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:
- C# – Static Members
- Perf penalty Static Constructor
- Static Constructors Demystified
- C# and beforefieldinit