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
-
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?
-
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?