Thursday 26 April 2012

Singletons

As per the word of Jon Skeet: http://csharpindepth.com/Articles/General/Singleton.aspx

A Simple Solution

public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();

static Singleton() { }

private Singleton() { }

public static Singleton Instance { get { return instance; } }
}

A Lazy Singleton

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() { }
}

No comments:

Post a Comment