See the following code. The input result is as follows:
The first output and the second output are both quite understandable. Why is the third output (Why are all static variables and static constructors not executed )?
Let's discuss it!
Static void Main (string [] args)
{
Console. WriteLine ("BaseA basea = new BaseA ();");
BaseA basea = new BaseA ();
Basea. MyFun ();
Console. WriteLine ();
Console. WriteLine ("BaseB baseb = new BaseB ();");
BaseB baseb = new BaseB ();
Baseb. MyFun ();
Console. WriteLine ();
Console. WriteLine ("BaseA basec = new BaseB ();");
BaseA basec = new BaseB ();
Basec. MyFun ();
Console. WriteLine ();
}
Public class BaseA
{
Public static MyTest a1 = new MyTest ("base class static variable ");
Public MyTest a2 = new MyTest ("base class instance variable ");
Static BaseA ()
{
MyTest a3 = new MyTest ("base class static constructor ");
}
Public BaseA ()
{
MyTest a4 = new MyTest ("base class instance constructor ");
}
Public virtual void MyFun ()
{
MyTest a5 = new MyTest ("base class instance function MyFun ");
}
}
Public class BaseB: BaseA
{
Public static MyTest b1 = new MyTest ("derived static variable ");
Public MyTest b2 = new MyTest ("derived instance variable ");
Static BaseB ()
{
MyTest b3 = new MyTest ("derived static constructor ");
}
Public BaseB ()
{
MyTest b4 = new MyTest ("derived instance constructor ");
}
Public new void MyFun ()
{
MyTest b5 = new MyTest ("derived instance function MyFun ");
}
}
Public class MyTest
{
Public MyTest (string info)
{
Console. WriteLine (info );
}
}
Author taoliuhua