All these implementations have a prvate and parameterless constructor.
First Version -- not thread safe
Class sealed Singleton
{
private static Singleton instance = null;
private Singleton(){}
public static Singleton Instance
{
get
{
if(instance == null)
{
Instance = new Singleton ();
}
return Instance;
}
}
}
Second Version --Simple thread safe
class Singleton
{
private static Singleton instance = null;
private static object padlock = new object();
private Singleton() {}
public static Singleton Instance
{
get
{
lock(padlock)
{
if(Instance == null)
{
instance = new Singleton();
}
return Instance;
}
}
}
}
Third Version -- Thread Safe
class Singleton
{
private static Singleton instance = new Singleton();
static Singleton(){}
private Singleton(){}
public static Singleton Instance
{
get
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
First Version -- not thread safe
Class sealed Singleton
{
private static Singleton instance = null;
private Singleton(){}
public static Singleton Instance
{
get
{
if(instance == null)
{
Instance = new Singleton ();
}
return Instance;
}
}
}
Second Version --Simple thread safe
class Singleton
{
private static Singleton instance = null;
private static object padlock = new object();
private Singleton() {}
public static Singleton Instance
{
get
{
lock(padlock)
{
if(Instance == null)
{
instance = new Singleton();
}
return Instance;
}
}
}
}
Third Version -- Thread Safe
class Singleton
{
private static Singleton instance = new Singleton();
static Singleton(){}
private Singleton(){}
public static Singleton Instance
{
get
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}