Wednesday 25 April 2012

A generic singleton using Lazy< T >

Carring on from the generic singleton post here.

Since we have .NET 4, we can now make use of Lazy< T >

public class Singleton< T > where T : class, new()
{
    private Singleton() {}

    private static readonly Lazy< T > instance = new Lazy< T >(() => new T());

    public static T Instance { get { return instance.Value; } } 
}