As I specified in the answers page: Derived. Foo(object) is printed - when choosing an overload, if there are any compatible methods declared in a derived class, all signatures declared in the base class are ignored - even if they're overridden in the same derived class! In other words, the compiler looks at methods which are freshly-declared in the most derived class (based on the compile-time type of the expression) and sees if any are applicable.
If they are, it uses the "best" one available. If none is applicable, it tries the base class, and so on. An overridden method doesn't count as being declared in the derived class See sections 7.4.3 and 7.5.5.1 of the C# 3 spec for more details Now as for exactly why it's specified like that - I don't URL1 makes sense to me that methods declared in the derived class take precedence over those declared in the base class, as otherwise you run into the "brittle base class" problem - adding a method in the base class could change the meaning of code using the derived class.
However, if the derived class is overriding the method declared in the base class, it's clearly aware of it, so that element of brittleness doesn't apply.
As I specified in the answers page: Derived. Foo(object) is printed - when choosing an overload, if there are any compatible methods declared in a derived class, all signatures declared in the base class are ignored - even if they're overridden in the same derived class! In other words, the compiler looks at methods which are freshly-declared in the most derived class (based on the compile-time type of the expression) and sees if any are applicable.
If they are, it uses the "best" one available. If none is applicable, it tries the base class, and so on. An overridden method doesn't count as being declared in the derived class.
See sections 7.4.3 and 7.5.5.1 of the C# 3 spec for more details. Now as for exactly why it's specified like that - I don't know. It makes sense to me that methods declared in the derived class take precedence over those declared in the base class, as otherwise you run into the "brittle base class" problem - adding a method in the base class could change the meaning of code using the derived class.
However, if the derived class is overriding the method declared in the base class, it's clearly aware of it, so that element of brittleness doesn't apply.
Forgive my ignorance , but why it is considering method with "object" type parameter as the best one , I think int is more close (like what is happening in 2 nd case) – Wondering May 12 '10 at 18:36 @Wondering: Yes, int is better - but the int overload is already overriding the method in the base class, so it isn't being considered: it doesn't count as being "freshly declared" in the derived class. That's the whole point of all of this explanation :) – Jon Skeet May 12 '10 at 18:47 Thanks Jon for all your time and help. I got your point.
– Wondering May 13 '10 at 4:35.
1 but the ans was not clear to me..thats why I asked the question. – Wondering May 12 '10 at 18:26 because when a method is overridden, it will search the derived class for any other methods with a compatible signature before using overridden methods from the base class. – derek May 12 '10 at 18:28.
It revolves around scope. In the first program, void Foo(int i) belongs to class Base. Class Derived merely redefines its behavior.
Foo(int i) is ignored because it's being "borrowed" and redefined via inheritance from class Base. If Foo(object o) did not exist, then Foo(int i) would be used. In the second program, void Foo(int i) is called because it properly belongs to class Derived (i.e.It's not being borrowed and overriden through inheritance) and has the best signature fit.
Good explanation. – Wondering May 13 '10 at 4:36.
It's because the methods signatures in the derived class are matched first before considering the base class signatures. Therefore, when you try: d. Foo(i); It attempts to match the method signature against your current class (not the base class).
It finds an acceptable match Foo(object) and doesn't further consider the base class method signatures. It's all about the compiler finding a matching method signature, and the search order it uses to find these signatures. -Doug.
The surprising part is that it doesn't include the overriding method in the derived class though. That's still a "method signature in the derived class" but it doesn't count as being declared in the derived class. – Jon Skeet May 12 '10 at 18:31 "It finds an acceptable match Foo(object)" but , Foo(int )is also acceptable match – Wondering May 12 '10 at 18:43 Yes, but as Jon notes, Foo(int) isn't declared in the derived class (even though it's overridden, which doesn't count).
– Doug May 12 '10 at 19:32.
It is issues like this that make method overloading one of my least favorite language features.
Overloading across classes in an inheritance hierarchy between multiple applicable signatures is always going to be hairy. I agree that ignoring the overridden method feels like a mistake - but I think the situation is generally unclear anyway. I prefer to either avoid inheritance, or avoid having multiple overloads which can be applicable for the same argument.
– Jon Skeet May 12 '10 at 18:56.
Even if they're overridden in the same derived class! In other words, the compiler looks at methods which are freshly-declared in the most derived class (based on the compile-time type of the expression) and sees if any are applicable. If they are, it uses the "best" one available.
If none is applicable, it tries the base class, and so on. An overridden method doesn't count as being declared in the derived class. See sections 7.4.3 and 7.5.5.1 of the C# 3 spec for more details.
Now as for exactly why it's specified like that - I don't know. It makes sense to me that methods declared in the derived class take precedence over those declared in the base class, as otherwise you run into the "brittle base class" problem - adding a method in the base class could change the meaning of code using the derived class. However, if the derived class is overriding the method declared in the base class, it's clearly aware of it, so that element of brittleness doesn't apply.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.