locked
Method overload with object parameters RRS feed

  • Question

  • if I define two methods:

    void foo(object a);
    void foo(string a);

    and then call foo("test") can I be sure that the second method is invoked?
    Tuesday, October 13, 2009 1:57 PM

Answers

  • No. Given that you have

    void foo(string a);
    void foo(object a);

    and call foo with object parameter (that contains string), then the overload that takes object will be invoked - the second one from the above - because "foo(object)" is a better function member for an object parameter.

    Moreover if you'd have a custom class with implicit conversion defined

    public class MyString
    {
        public string Data {get;set;};
        public static implicit operator string(MyString o)
        {
            return o.Data;
        }
    }

    then you could have used it in your foo invocations - foo(string) would be used. In this case, both conversions are valid - MyString can be converted to object and it can be converted to string with implicit conversion we provided - but custom string conversion is consitered to be a better one.


    codevanced.net
    • Edited by codevanced Tuesday, October 13, 2009 2:43 PM
    • Proposed as answer by jgalley Tuesday, October 13, 2009 4:08 PM
    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 6:44 AM
    Tuesday, October 13, 2009 2:32 PM

All replies

  • Try it for yourself, just put a breakpoint in, whatever happens the first time always will. Another interesting one to try would be this:

    object obj = "test";
    foo(obj);

    Tuesday, October 13, 2009 2:01 PM
  • well, I do not want to test it :) I tried to find compiler rules on this case but failed.

    I want to be sure that there is a compiler rule and would be happy to see a reference to documentation. I do not want to hope that it works like the test showed
    Tuesday, October 13, 2009 2:03 PM
  • Here is the Method Overload Resolution from the C# Language Specification.
    I always try to Keep it Sharp & simple.
    Tuesday, October 13, 2009 2:03 PM
  • Welcome to C# spec - 14.4.2 Overload resolution

    Compiler looks for the "better function member" which is:

    "...Given an argument list A with a sequence of argument types {A1, A2, …, AN} and two applicable function members MP and MQ with parameter types {P1, P2, …, PN} and {Q1, Q2, …, QN, MP}, after expansion and type argument substitution,is defined to be a better function member than MQ if
    • for each argument, the implicit conversion from AX to PX is not worse than the implicit conversion from AX to QX, and
    • for at least one argument, the conversion from AX to PX is better than the conversion from AX to QX..."



    codevanced.net
    Tuesday, October 13, 2009 2:14 PM
  • ok. so foo(string) will be invoked on foo("test") as well as on foo(a) where a is of type object with value "test".


    Tuesday, October 13, 2009 2:25 PM
  • No. Given that you have

    void foo(string a);
    void foo(object a);

    and call foo with object parameter (that contains string), then the overload that takes object will be invoked - the second one from the above - because "foo(object)" is a better function member for an object parameter.

    Moreover if you'd have a custom class with implicit conversion defined

    public class MyString
    {
        public string Data {get;set;};
        public static implicit operator string(MyString o)
        {
            return o.Data;
        }
    }

    then you could have used it in your foo invocations - foo(string) would be used. In this case, both conversions are valid - MyString can be converted to object and it can be converted to string with implicit conversion we provided - but custom string conversion is consitered to be a better one.


    codevanced.net
    • Edited by codevanced Tuesday, October 13, 2009 2:43 PM
    • Proposed as answer by jgalley Tuesday, October 13, 2009 4:08 PM
    • Marked as answer by Bin-ze Zhao Monday, October 19, 2009 6:44 AM
    Tuesday, October 13, 2009 2:32 PM
  • got you. my mistake
    Tuesday, October 13, 2009 2:41 PM