The C# 4.0 specs read:
When a virtual method is invoked, the runtime type of the instance for
which that invocation takes place determines the actual method
implementation to invoke. In a nonvirtual method invocation, the
compile-time type of the instance is the determining factor.
At first, I thought this had something to do with initialization. For example, given two initializations:
BaseClass bcDerived = new Derived(); vs BaseClass bcBase = new BaseClass();
and an overload in a helper class:
public virtual void Method(Derived d)
{
Console.WriteLine("Result = derived called");
}
public virtual void Method(BaseClass d)
{
Console.WriteLine("Result = base called");
}
Method invokation is not impacted by the virtual keyword in this case. Regardless of having marked virtual, the least derived overload is called. Only during override in the Derived class does the method invocation change.
So, what do “runtime type” and “compile-time type” mean? How do they impact method invocation?
This is more a matter of virtual vs. non-virtual methods, and how the invocation occurs. The portion of the spec you are quoting deals with method calls on a variable – calling
bcDerived.SomeMethod(), not callingfoo.SomeMethod(bcDerived).The specification you are quoting refers to the case where you have non-virtual methods:
Then the method called will be determined by the compiler, at compile time, so doing:
Will cause this to call
A.Foo(), no matter what subclass of A is being referred to bysomeInst, since this is a non-virtual method.If you have a virtual method, however, the
callvirtinstruction is specified by the compiler, which moves the decision to runtime. This means that:Will call
B.Bar(), notA.Bar().In your case, you’re not calling a virtual method (in the sense that the spec is referring to), but doing standard method resolution. 7.5.3 of the C# Spec deals with Overload resolution in detail. In your case, the argument list (
bcDerived) is inspected by the compiler, and seen to be defined as typeBaseClass. The “best match” for this is going to bepublic virtual void Method(BaseClass d)as the the parameter list directly matches the argument list, so that is used at compile time.Method overload resolution, if you look at the specification, doesn’t directly take virtual method calls into effect – it only looks at implicit conversions between types.