Paraphrase from C # in depth : There are various ways to implement a singleton pattern in C #, starting with Unsafe for a fully lazily loaded, thread safe, simple and highly efficient version.
The best version is using .NET 4 Lazy type:
public sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }
It just works well. It also allows you to check if an instance with IsValueCreated has yet been created if you need it.
Gang
source share