Singleton by Jon Skeet clarification

  1. No, this is nothing to do with closures. A nested class has access to its outer class’s private members, including the private constructor here.

  2. Read my article on beforefieldinit. You may or may not want the no-op static constructor – it depends on what laziness guarantees you need. You should be aware that .NET 4 changes the actual type initialization semantics somewhat (still within the spec, but lazier than before).

Do you really need this pattern though? Are you sure you can’t get away with:

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();
    public static Singleton Instance { get { return instance; } }

    static Singleton() {}
    private Singleton() {}
}

Leave a Comment