I was reading a tutorial for implementing Singleton and the code is
public class Singleton
{
private static readonly Singleton instance = new Singleton();
static Singleton()
{
Console.WriteLine("Static");
}
private Singleton()
{
Console.WriteLine("Private");
}
public static Singleton Instance { get { return instance; } }
public void DoSomething() {
//this must be thread safe
}
}
When I write Singleton.Instance, the output is
Private
Static
I was expecting it to be
Static
Private
Reason being that when I read a MSDN tutorial "https://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx"
I saw that public constructor was called after the static constructor.
Why is there a difference?