I am trying to understand singleton pattern in C#. I got 1 structure for that but i have some doubt in the implementation. Since I'm new to C#, i want to know how this is working.
public class Class1
{
public static void Main(string[] args)
{
Singleton.Object.Func1();
}
}
public sealed class Singleton
{
private static readonly Singleton _obj = new Singleton();
static Singleton() { } // Make sure it's truly lazy
private Singleton() { } // Prevent instantiation outside
public static Singleton Object { get { return _obj; } }
public void Func1() { }
}
Is this pattern correct for a singleton class?
What is the purpose of this line "static Singleton() { } // Make sure it's truly lazy"
How the class identify only 1 instance is created in a particular time