开发者

Overloads Done At Compile Time

I was just reading this answer

Which overload is called and how?

by Jon Skeet and I just done understand how overload resolution gets done at compile time - How is that possible?? You dont know the type of the object till you run??

I always thought that all method calls were done at run time (late binding)

What are the exceptions to that??

I ll give开发者_如何学Python an example:

public void DoWork(IFoo)
public void DoWork(Bar) 

IFoo a = new Bar();
DoWork(a)

Which method gets called here and why?


When a method call is encountered by the compiler the types of all the parameters are known because C# is a statically-typed language: all expressions and variables are of a particular type and that type is definite and known at compile time.

This is ignoring dynamic which slightly complicates things.

Edit: This is a response to your edit. For clarity, I translated your code into the following:

interface IFoo { }
class Bar : IFoo { }
class Test {
    public void DoWork(IFoo a) { }
    public void DoWork(Bar b) { }
}

class Program {
    static void Main(string[] args) {
        IFoo a = new Bar();
        Test t = new Test();
        t.DoWork(a);
    }
}

You are asking which method is called here (Test.DoWork(IFoo) or Test.DoWork(Bar)) when invoked as t.DoWork(a) in Main. The answer is that Test.DoWork(IFoo) is called. This is basically because the the parameter is typed as an IFoo. Let's go the specification (§7.4.3.1):

A function member is said to be an applicable function member with respect to an argument list A when all of the following are true:

The number of arguments in A is identical to the number of parameters in the function member declaration.

For each argument in A, the parameter passing mode of the argument (i.e., value, ref, or out) is identical to the parameter passing mode of the corresponding parameter, and

for a value parameter or a parameter array, an implicit conversion (§6.1) exists from the argument to the type of the corresponding parameter, or

for a ref or out parameter, the type of the argument is identical to the type of the corresponding parameter. After all, a ref or out parameter is an alias for the argument passed.

The issue here (see the bolded statement) is that there is no implicit conversion from IFoo to Bar. Therefore, the method Test.DoWork(Bar) is not an applicable function member. Clearly Test.DoWork(IFoo) is an applicable function member and, as the only choice, will be chosen by the compiler as the method to invoke.


I think you are confusing overload resolution with virtual method dispatch. Yes, the runtime type of the object will determine which method to run but the method has already been bound by the compiler. This is a type-safe way of allowing polymorphic behavior without all the flexibility and danger of true late-binding.

The compiler will determine which method overload in to call based on the types of the arguments as written in your code.


Virtual methods (including calls through an interface) are dispatched on the type of the receiver at runtime (late binding).

Nonvirtual methods are resolved at compile time, using the compile-time type of the reference. This is why shadowing (the new modifier) results in different behaviour depending on whether you call the shadowed method through a base class reference or a derived class reference.

In all cases, overload resolution uses the compile-time types of the arguments. Only the receiver -- that is, the x in a call of the form x.SomeMethod(y, z) -- is considered for late binding. Thus, if y is typed as object, and the only overloads are string y and int y, the compiler will error, even if at runtime y would actually be a string or int -- because it's only considering the compile-time type (the declared type of the variable).


The only thing decided at runtime is "virtual" functions, you call the appropriate function for your object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜