Singleton: Ensure a class has only one instance and provide a global point of access to it.
{
private static volatile SingletonClassName Object1 = null;
private static object SynRoot = new object();
private SingletonClassName()
{
// do nothing
}
public static SingletonClassName GetInstance()
{
SingletonClassName RetVal = null;
// check SingletonClass object is null or not. If it is null then create Object
if (SingletonClassName.Object1 == null)
{
// lock code block to avoid dead lock condition and it is required in multi-threaded environment
lock (SingletonClassName.SynRoot)
{
// initialize object
SingletonClassName.Object1 = new SingletonClassName();
}
}
// return Singleton class object
RetVal = SingletonClassName.Object1;
return RetVal;
}
}