public SingleTone GetInstanceLazy() { if (instance == null) { instance = new SingleTone(); } return(instance); }
public SingleTone GetInstanceThreadSafe() { Contract.Ensures(Contract.Result <SingleTone>() != null); lock (lockThis) { if (instance == null) { instance = new SingleTone(); } return(instance); } }
static void Main(string[] args) { for (int i = 0; i < 10; i++) { var myObject = new SingleTone().GetInstanceEager(); Console.WriteLine(myObject.time.ToString()); } for (int i = 0; i < 10; i++) { var myObject = new SingleTone().GetInstanceEager(); Console.WriteLine(myObject.time.ToString()); } for (int i = 0; i < 10; i++) { var myObject = new SingleTone().GetInstanceThreadSafe(); Console.WriteLine(myObject.time.ToString()); } }
public SingleTone GetInstanceEager() { instance = new SingleTone(); return(instance); }