I've come across a C# behavior that I would like to understand.
Why in both cases instance constructor runs first?
class Program
{
public static void Main()
{
//Singleton s = new Singleton(); case 1
var test = Singleton.Instance; // case 2
}
}
class Singleton
{
static readonly Singleton _instance = new Singleton();
public static Singleton Instance { get { return _instance; } }
private Singleton()
{
Console.WriteLine("Instance Constructor");
}
static Singleton()
{
Console.WriteLine("Static Constructor");
}
}
Output:
Instance Constructor
Static Constructor