After reading this article and that article - I got confused.
It says:
If there are two methods at different levels of the hierarchy, the first will be selected “deeper”, even if it is not the “best member function” to call.
Also -
It turns out that if you override the method of the base class in a child class that is not considered to be a declaration of it.
Now back to my question:
Case 1
public class Base { public virtual void Foo(int x) { "1".Dump();} } public class Child : Base { public void Foo(object x) { "3".Dump();} public override void Foo(int x) { "2".Dump();} } void Main() { Child c = new Child(); c.Foo(10);
OK. According to the article
the “deeper” will be selected first, even if it is not a “better function” and does not account for overriding ...
So, this is correct, and the program returns "3". (running Foo(object x) )
Change line of order 1 line :
Case 2
public class Base { public virtual void Foo(int x) { "1".Dump();} public void Foo(object x) { "3".Dump();}
Now it emits a "2".
Now change all int to an object and the whole object to int:
Case 3
public class Base { public virtual void Foo(object x) { "1".Dump();} public void Foo(int x) { "3".Dump();} } public class Child : Base { public override void Foo(object x) { "2".Dump();} } void Main() { Child c = new Child(); c.Foo(1);
Questions :
Question # 1: in case 2, Child inherited Foo(object x) from his father. And he also redefines the method.
but , we simply did not say that:
It turns out that if you override the method of the base class in a child class that is not considered to be its declaration
???
in fact, we also did not declare the inherited function ... so what is the rule here in this situation?
Question No. 2: in case 3, Child inherited Foo(int x) from his father. And he also redefines the method.
but now he selects the function of his father ....
it seems that override wins only if it has an exact match.
again, what is the rule here in this situation?