It depends more on virtual and non-virtual methods and on how the call is made. The part of the specification that you are quoting handles method calls to call the variable bcDerived.SomeMethod() that does not call foo.SomeMethod(bcDerived) .
The specification you are quoting refers to the case where you have non-virtual methods:
public class A { public void Foo() { Console.WriteLine("A.Foo"); } public virtual void Bar() { Console.WriteLine("A.Bar"); } } public class B : A { public new void Foo() { Console.WriteLine("B.Foo"); } public override void Bar() { Console.WriteLine("B.Bar"); } }
Then the called method will be determined by the compiler at compile time:
A someInst = new B(); someInst.Foo();
This will call A.Foo() , regardless of which subclass A is referenced by someInst , since this is not a virtual method.
If you have a virtual method, the callvirt statement is set by the compiler, which moves the solution at run time. It means that:
someInst.Bar();
Call B.Bar() , not A.Bar() .
In your case, you do not call the virtual method (in the sense referred to in the specification), but when you execute the standard method. 7.5.3 C # Spec describes in detail overload resolution. In your case, the argument list ( bcDerived ) is checked by the compiler and treated as a BaseClass type. The βbest fitβ for this would be the public virtual void Method(BaseClass d) , since the parameter list directly matches the argument list, which is why it is used at compile time.
Enabling method overloads, if you look at the specification, does not directly accept virtual method calls - it only looks at implicit conversions between types.
Reed copsey
source share