在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数。这是由于C#的运行机制造成的,原因如下:
新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类。构造函数的执行则相反,先执行父类的,再执行本身的。
如果类包含对父类虚函数的重载,在父类构造函数中调用虚函数时,会执行函数表中继承类的虚函数,这就有问题了。
下面是个测试例子:
public class Base
{
public Base()
{
System.Console.WriteLine("Base.Base");
ABitDangerousCall();
} public virtual void ABitDangerousCall()
{
System.Console.WriteLine("Base.ABitDangerousCall");
} private class Inner
{
public Inner()
{
System.Console.WriteLine("Base.Inner.Inner");
}
}
private Inner inner = new Inner();
} class Derived : Base
{
public Derived()
{
System.Console.WriteLine("Derived.Derived");
ctorInitializedMember = ;
} // ctorInitializedMember is default initialized to zero before the constructor initializes it.
private int ctorInitializedMember;
private int derivedInt = ; public override void ABitDangerousCall()
{
System.Console.WriteLine(String.Format("Derived.ABitDangerousCallctorInitializedMember={0} derivedInt={1}", ctorInitializedMember, derivedInt));
} private class Inner
{
public Inner()
{
System.Console.WriteLine("Derived.Inner.Inner");
}
}
private Inner inner = new Inner();
} class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
Console.Read();
}
}
如果真有在构造函数中调用虚函数的必要,应将该类或该重载的虚函数设为seal,以避免被继承。