I read Jeffrey Richter - CLR via C# and decide to make some test applications. I need some help to understand what exactly happen and why. And yes i know, public property is bad idea, but my question not about code style. So:
class ClassA
{
public static int val1;
static ClassA
{
val1 = 10;
}
}
class ClassB : ClassA
{
public static int val2;
static ClassB
{
val1 = 15;
}
}
And now we call output to console in this order:
Console.WriteLine(ClassB.val1);
Console.WriteLine(ClassB.val2);
Console.WriteLine(ClassB.val1);
Output is:
10
0
15
So, as i understand, compiler will make call of static ctor when static member of that type used for first time. Right before usage.
So why it call static ctor of ClassB not at the first line? It's all because static members not inherit, so it just call base type in first line? Explain plz. Thanks.