Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runtime type vs compile-time type method invocation

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?

like image 979
P.Brian.Mackey Avatar asked Jan 21 '13 16:01

P.Brian.Mackey


People also ask

What is the difference between runtime and compile time?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is compile time type?

The declared type or compile-time type of a variable is the type that is used in the declaration. The run-time type or actual type is the class that actually creates the object.

What is runtime type in Java?

Runtime Type Identification in Java can be defined as determining the type of an object at runtime. It is extremely essential to determine the type for a method that accepts the parameter of type java.


1 Answers

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 calling 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 method called will be determined by the compiler, at compile time, so doing:

A someInst = new B();
someInst.Foo();

Will cause this to call A.Foo(), no matter what subclass of A is being referred to by someInst, since this is a non-virtual method.

If you have a virtual method, however, the callvirt instruction is specified by the compiler, which moves the decision to runtime. This means that:

 someInst.Bar();

Will call B.Bar(), not A.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 type BaseClass. The "best match" for this is going to be public 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.

like image 150
Reed Copsey Avatar answered Oct 15 '22 05:10

Reed Copsey