Articles → .NET DESIGN PATTERN → Singleton Design Pattern In C#
Singleton Design Pattern in C#
Purpose
Implementation
- Make the constructor of the class private
- Create a method that returns the instance of the class
public class SingletonClass
{
static SingletonClass _singletonClass;
private SingletonClass()
{
}
public static SingletonClass GetInstance()
{
if (_singletonClass == null)
_singletonClass = new SingletonClass();
return _singletonClass;
}
}
Calling The Singleton Class
SingletonClass singletonClass = SingletonClass.GetInstance();