How is method group overload resolution different to method call overload resolution?

Posted by thecoop on Stack Overflow See other posts from Stack Overflow or by thecoop
Published on 2010-06-14T11:10:21Z Indexed on 2010/06/14 11:12 UTC
Read the original article Hit count: 307

Filed under:
|
|
|

The following code doesn't compile (error CS0123: No overload for 'System.Convert.ToString(object)' matches delegate 'System.Converter<T,string>'):

class A<T> {
    void Method(T obj) {
        Converter<T, string> toString = Convert.ToString;
    }
}

however, this does:

class A<T> {
    void Method(T obj) {
        Converter<T, string> toString = o => Convert.ToString(o);
    }
}

intellisense gives o as a T, and the Convert.ToString call as using Convert.ToString(object). In c# 3.5, delegates can be created from co/contra-variant methods, so the ToString(object) method can be used as a Converter<T, string>, as T is always guarenteed to be an object.

So, the first example (method group overload resolution) should be finding the only applicable method string Convert.ToString(object o), the same as the method call overload resolution. Why is the method group & method call overload resolution producing different results?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET