According to Jon Skeet's artice C# and beforefieldinit and discussion in When is a static constructor called in C#? static constructor must be called before first call to a method of the class.
For some reason following code does not exhibit this behavior:
namespace AbstractAndStatic
{
class Program
{
static void Main(string[] args)
{
StaticClass.Equals(1,2);
StaticClass.foo();
}
}
static class StaticClass : Object
{
public static void foo()
{
Console.WriteLine("Static");
}
static StaticClass()
{
Console.WriteLine("static constructor");
}
}
class TestClass
{
public void deb()
{
Console.WriteLine("Test Class Debug");
}
}
}
I am debugging the above code using the Visual Studio Debugger. When the statement StaticClass.Equals(1,2);
gets executed in the Main method the static Constructor is not getting called but when StaticClass.foo();
is executed it calls static constructor and then call the foo method.
I am little confused as why it didn't get called the the first time when executing StaticClass.Equals(1,2);
.