public class MySingletonWithDoubleCheck {
private static object myLock = new object();
private static MySingletonWithDoubleCheck mySingleton = null;
private MySingletonWithDoubleCheck() {
public static MySingletonWithDoubleCheck GetInstance() {
if (mySingleton == null) {
if (mySingleton == null) {
mySingleton = new MySingletonWithDoubleCheck();
Console.WriteLine("MySingletonWithDoubleCheck "+DateTime.Now.ToString());
public class MySingletonWithLazy
private static readonly Lazy<MySingletonWithLazy> _mySingleton = new Lazy<MySingletonWithLazy>(() => new MySingletonWithLazy());
private MySingletonWithLazy() { }
public static MySingletonWithLazy GetInstance()
return _mySingleton.Value;
Console.WriteLine("MySingletonWithLazy "+DateTime.Now.ToString());
public static void Main(string[] args)
MySingletonWithDoubleCheck instance = MySingletonWithDoubleCheck.GetInstance();
MySingletonWithLazy instance1 = MySingletonWithLazy.GetInstance();