Singletons in C#, Static Constructor vs Static Initialization


Not a long time ago i posted a article about how to implement a static constructor. As a example I used a singleton to show how the static contstructor works and how it is implemented. The result was a discussion about singletons, what they are good for and how it can be implemented in C#. There is the standard way (how it is normally done) and also a lot easier way for C# with a static constructor or a static initialization.

 

The classic singleton

public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();

private Singleton() {}

public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}

return instance;
}
}
}

In every object oriented language (or these i know) a Singleton looks nearly (more or less) like the code above. The Singleton restrict the instantiation of a class to one object. This is provided by a static field which holds the instance. If the instance which is stored in the static field is still null, a new object will be created and assigned to the static variable. The code which does the instantiation is locked so it also work in a multi threaded environment (double check locking). Too much code for you? 

Singleton with static Initialization

public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();

private Singleton(){}

public static Singleton Instance
{
get
{
return instance;
}
}
}

C# allows you to initialize the static field directly (and it also work properly)! The instantiation does occur when instance property is called the first time, so it is some sort of lazy instantiation. .NET takes control that it also work in multi threaded environments, so you have nothing to do with it. But you will still have a problem with that solution if you want to execute custom code before instantiation! With a static constructor, you can solve this problem!

Singleton with static constructor

public sealed class Singleton
{
private static readonly Singleton instance;

private Singleton() { }

static Singleton()
{
instance = new Singleton()
}

public static Singleton Instance
{
get
{
return instance;
}
}
}

It allows you to execute custom code before the singleton is instantiated. But the complexity is still handled by the .net framework, so you don’t have to implement a double check locking also when you work in multi threaded environments. The static constructor is only executed, when the instance property is called the first time. So both variations allow you to implement a thread safe singleton where the complexity is hidden in the .net framework, you not even need a if statement!

38 thoughts on “Singletons in C#, Static Constructor vs Static Initialization

  1. Howdy, i read your weblog occasionally and i own a similar one and i was just wondering if you get lots of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much recently it is driving me mad so any assistance is very much appreciated.

Comments are closed.