24

I have the following classes:

public class BaseRepository
{
    public virtual void Delete(int id)
    {
        Console.WriteLine("Delete by id in BaseRepository");
    }
}

public class EFRepository: BaseRepository
{
    public override void Delete(int id)
    {
        Console.WriteLine("Delete by Id in EFRepository");
    }

    public void Delete(object entity)
    {
        Console.WriteLine("Delete by entity in EFRepository");
    }
}

Then I use it like:

var repository = new EFRepository();
int id = 1;
repository.Delete(id);

Why in that case only EFRepository.Delete(object entity) will call?

2
  • object can hold an int.
    – Travis J
    Sep 1, 2016 at 22:20
  • @TravisJ I know it well but the question was a little bit different Sep 5, 2016 at 8:53

2 Answers 2

28

Basically, the way method invocation works in C# is that the compiler looks at the most derived class first, and sees whether any newly declared methods (not including overrides) are applicable for the arguments for the call. If there's at least one applicable method, overload resolution works out which is the best one. If there isn't, it tries the base class, and so on.

I agree this is surprising - it's an attempt to counter the "brittle base class" issue, but I would personally prefer that any overridden methods were included in the candidate set.

Method invocation is described in section 7.6.5.1 of the C# 5 specification. The relevant parts here is:

  • The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set. Furthermore, if C is a class type other than object, all methods declared in an interface type are removed from the set. (This latter rule only has affect when the method group was the result of a member lookup on a type parameter having an effective base class other than object and a non-empty effective interface set.)

And in the member lookup part of 7.4, override methods are explicitly removed:

Members that include an override modifier are excluded from the set.

4
  • Thank you for the explanation. So, there is no way to call the method with int parameter directly, right? Aug 30, 2016 at 17:51
  • 1
    @MegaTron: You'd have to cast to BaseRepository first.
    – Jon Skeet
    Aug 30, 2016 at 17:52
  • Thanks. I think it will be better to change that design Aug 30, 2016 at 17:53
  • Does the phrase "newly declared methods (not just overridden ones)" exclude overridden methods? If so, the "just" makes it confusing.
    – piedar
    Aug 31, 2016 at 14:01
0

Because: public override void Delete(int id) will only override the base class method which one : public virtual void Delete(int id).

While public void Delete(object entity) is the EFRepository class method, so when you execute method from EFRepository object it will invoke to own method which is nothing but the public void Delete(object entity)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.