Singleton by Jon Skeet clarification

public sealed class Singleton
{
    Singleton() {}

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

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested() {}
        internal static readonly Singleton instance = new Singleton();
    }
}

I wish to implement Jon Skeet’s Singleton pattern in my current application in C#.

I have two doubts on the code

  1. How is it possible to access the outer class inside nested class? I mean

    internal static readonly Singleton instance = new Singleton();
    

    Is something called closure?

  2. I am unable to understand this comment

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    

    what does this comment suggest us?

2 Answers
2

Leave a Comment