Search code examples
c#inheritancemethod-resolution-order

method resolution with base types


My situation is this:

public class InheritedClass : BaseClass
{
    public override void SomeMethod()
    {
        AnotherMethod();
    }
    public override void AnotherMethod()
    {
    }
}

public class BaseClass
{
    public virtual void SomeMethod()
    { }
    public virtual void AnotherMethod()
    { }
}

So which method is called when I call InheritedClassInstance.SomeMethod? Does it call InheritedClassInstance.AnotherMethod, or the BaseClass's AnotherMethod?


Solution

  • It calls InheritedClassInstance.AnotherMethod()

    If you wanted it to call the base class AnotherMethod() you would write base.AnotherMethod()