Saturday, August 9, 2008

How .NET supports the Singleton

The following example shows an easy way to implement Singleton class using the .NET Framework.


Sealed class Singleton {
public static readonly Singleton = new Singleton();
}


1. The Framework guarantees thread safety on static type initialization.

2. The Singleton is still created using lazy initialization in order not to consume memory when the object is not needed.

No comments: