Vanguard Coder

Simple Life of a Keen Developer

Singleton Gymnastics

leave a comment

As books on C#5.0 (using .Net 4.5 framework) are being released, I remember during the pre-.Net 4.0 dates the awesome amounts of gymnastics that were done around the simplest design pattern – the Singleton.
Some involved double checking (in and outside the lock), setting up Memory Barriers. Most of this discussion can be read here. Finally System.Lazy came about and the rest was history.

I wonder wonder what’ll the most popular feature in that C#5.0 uses with .Net 4.5


public sealed class IrritatingConstructor
{
    private static readonly Lazy<IrritatingConstructor> lazy =
        new Lazy<IrritatingConstructor>(() => new IrritatingConstructor());

    public static IrritatingConstructor Instance
    {
        get
        {
            return lazy.Value;
        }
    }

    private IrritatingConstructor()
    {
    }
}

Written by zkashan

July 3rd, 2012 at 7:50 pm

Posted in Uncategorized

Leave a Reply