I want to create a Singleton object. On MSDN and other places I found the same example like the one here:
It works, that's not the problem. But why is there a seperate object used for locking? Why not just use lock(_instance) instead of lock(_lock)?Code:public sealed class Singleton { static Singleton _instance = null; static readonly object _lock = new object(); Singleton() { } public static Singleton Instance { get { lock (_lock) { if (_instance==null) { _instance = new Singleton(); } return _instance; } } } }




Reply With Quote