How do we call a virtual method from another method in the base class even when the current instance is of a derived-class?

两盒软妹~` 提交于 2019-12-01 18:41:42

No you cannot do that, the purpose of virtual methods is that derived classes can override the implementation and that the implementation is used even when called from base classes.

If that causes problems then the code you need to run should not be in a virtual method.

Henrik

Obvious solution:

    public virtual void Method1()
    {
      Console.WriteLine("Method1 in Base class.");
      this.Method2Private( );
    }

    private void Method2Private()
    {
      Console.WriteLine( "Method2 in Base class." );
    }

    public virtual void Method2()
    {
      Method2Private();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!