// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; namespace Singleton { // **** double lock singleton pattern is not thread safe - never use it **** public class DoubleLockSingleton { private static DoubleLockSingleton instance; private static readonly object mylock = new object(); private int someState; private DoubleLockSingleton() { someState = 123; } public static DoubleLockSingleton Instance { get { if(instance == null) { lock(mylock) { if(instance == null) { instance = new DoubleLockSingleton(); } } } return instance; } } public void SomeMethod() { Console.WriteLine(someState); } } }