public class SingleTonTest
{
public static void Test()
{
MySingleton.Instance.DO();
MySingleton.Instance.DO();
}
}
public class MySingleton
{
private static MySingleton instance = null;
static MySingleton()
{
Console.WriteLine("Static Constructor called");
}
private MySingleton()
{
Console.Write("Private conatructor called");
}
// Lock synchronization object
private static object myLock = new object();
public static MySingleton Instance
{
get
{
lock (myLock)
{
if (instance == null)
instance = new MySingleton();
return instance;
}
}
}
public void DO()
{
Console.WriteLine("Do your work");
}
}
{
public static void Test()
{
MySingleton.Instance.DO();
MySingleton.Instance.DO();
}
}
public class MySingleton
{
private static MySingleton instance = null;
static MySingleton()
{
Console.WriteLine("Static Constructor called");
}
private MySingleton()
{
Console.Write("Private conatructor called");
}
// Lock synchronization object
private static object myLock = new object();
public static MySingleton Instance
{
get
{
lock (myLock)
{
if (instance == null)
instance = new MySingleton();
return instance;
}
}
}
public void DO()
{
Console.WriteLine("Do your work");
}
}