I read Jeffrey Richter - CLR through C # and decided to make some test applications. I need help to understand what exactly is happening and why. And yes, I know, public ownership is a bad idea, but my question is 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 the console output in the following order:
Console.WriteLine(ClassB.val1);
Console.WriteLine(ClassB.val2);
Console.WriteLine(ClassB.val1);
Output:
10
0
15
So, as I understand it, the compiler will make the call static ctorwhen the static member of this type is used for the first time. Right before use. So why static ctor ClassBdoesn't it call on the first line? Is this all because static members are not inherited, so just name the base type in the first line? Explain plz. Thank.