Which C# method overload is chosen?

后端 未结 4 1522
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 01:23

Why is the generic method called when both overloads would match?

public static void method1(object obj)
{
    Console.WriteLine(\"Object\");
}

public stati         


        
4条回答
  •  被撕碎了的回忆
    2020-12-10 02:15

    How method overloading works

    To find the matching signature of a method for a call, the compiler search in the type hierarchy from bottom to top as well in virtual table:

    • First in the class hierarchy,
    • Then in the interface hierarchy.

    Because classes prevail on interfaces.

    Indeed, before being of type of an interface, an object is of type of a class first of all.

    And non generic signatures prevail over generic as reality and facts prevail over abstraction, unless using the generic parameter allow a call on the more specialized type of instance.

    Applying the theory to the question

    This call:

    method1("xyz");
    

    Match perfectly with:

    void method1(T t) { }
    

    Before matching with:

    void method1(object obj)
    

    Because string is a specialized object and it can be used as a generic parameter to be more acurate.

    On the other side, if you write:

    void method1(string obj) { }
    
    void method1(T t) { }
    

    The first method is so called.

    Case study

    var instance = new List();
    
    MyMethod(instance);
    MyMethod((IEnumerable) instance);
    MyMethod(instance);
    MyMethod((object)instance);
    
    void MyMethod(List instance) { }
    
    void MyMethod(IEnumerable list) { }
    
    void MyMethod(T instance) { }
    
    void MyMethod(object instance) { }
    
    • The first call calls the first method because instance is type of List (type matching).

    • The second call calls the second method because of the side cast (implementation).

    • The third call calls the third method because of the generic parameter specified to act on (templating).

    • The fourth call calls the fourth method because of the down cast (polymorphism).

提交回复
热议问题