class Program
{
static void Main()
{
Console.WriteLine("enter main method");
Console.WriteLine(Foo.X);
Console.ReadKey();
}
}
class Foo
{
public static Foo Instance = new Foo();
public static int X = 3;
//static Foo()
//{
// Console.WriteLine("static constructor");
// Console.WriteLine(X);
//}
Foo()
{
Console.WriteLine("instance constructor");
}
}
Why the program begin with the code in Foo, after that, the main method begin execute.
The result is:
instance constructor
enter main method
3
Anyone can help?
Foo has a static variable called Instance.
This static variable is intialized before Main is executed, which causes the constructor Foo() to be called