Override methods not included in overload search????
-
Why!?! I found this interesting bit of text in the C# specification at MSDN here[^], when trying to find out why my code isnt working: First, the set of all accessible members named N declared in T and the base types of T is constructed. Declarations that include an override modifier are excluded from the set. Can anyone explain to me why? It seems illogical. Consider the following:
class A {
public virtual int Foo () { return 1; }
}
class B : A {
new int Foo () { return 2; }
static void Main () { System.Console.WriteLine (new C ().Foo ()); }
}
class C : B {
public override int Foo () { return 3; }It outputs 2, because the code is executed in B. If B didn't define a Foo() method, it would output 1. Logically (well, to me anyway) it should output 3! -- Dave
-
Why!?! I found this interesting bit of text in the C# specification at MSDN here[^], when trying to find out why my code isnt working: First, the set of all accessible members named N declared in T and the base types of T is constructed. Declarations that include an override modifier are excluded from the set. Can anyone explain to me why? It seems illogical. Consider the following:
class A {
public virtual int Foo () { return 1; }
}
class B : A {
new int Foo () { return 2; }
static void Main () { System.Console.WriteLine (new C ().Foo ()); }
}
class C : B {
public override int Foo () { return 3; }It outputs 2, because the code is executed in B. If B didn't define a Foo() method, it would output 1. Logically (well, to me anyway) it should output 3! -- Dave
Hi David. w.r.to, the code sample,,,, class B : A { new int Foo () { return 2; } static void Main () {System.Console.WriteLine (new C ().Foo ()); } } This is an apt example for Hiding Methods. The statement 'new int Foo () { return 2; }' in Class B , informs the compiler that the base class member has been hidden and hence it continues executing the method in B. w.r.to the statement, If B didn't define a Foo() method, it would output 1....check out it would output only '3'. I hope is this what you wanted to clarify. regards, Manivannan.P