ReSharper warning: Virtual member call in a constructor

1.构造函数的执行顺序是:基类--->派生类

2.如果虚方法被重写后,由于基类中调用了虚方法,此时调用的是最外层的被重写后的虚方法,此时可能会发生异常

举例:

class Parent
{
    public Parent()
    {
        DoSomething();
    }
    protected virtual void DoSomething() 
    {
    }
}

class Child : Parent
{
    private string foo;
    public Child() { foo = "HELLO"; }
    protected override void DoSomething()
    {
        Console.WriteLine(foo.ToLower());
    }
}

如果实例化Child会抛出NullReferenceException异常。

详情请参考:http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor

你可能感兴趣的:(ReSharper warning: Virtual member call in a constructor)