Goal
Restrict instanciation of a class to one object.
UML
![]()
C#
Having a sealed class and private constructor are essential to forbid indirect creation of objects of this class. The actual creation of the object is deferred to the first call to the Instance property. For a complete analysis and also some alternatives with full lazy creation, have a look here (you will see the things are actually not as simple as they look like)
public sealed class $classname$
{
private static readonly $classname$ _instance = new $classname$;
private static $classname$() { } // lazy instantiation
private $classname$() { }
public static $classname$ Instance
{
get { return instance; }
}
}







