What is a method group in C#?

前端 未结 5 2004
夕颜
夕颜 2020-11-22 01:46

I have often encountered an error such as \"cannot convert from \'method group\' to \'string\'\" in cases like:

var list = new List();
// ...          


        
5条回答
  •  情话喂你
    2020-11-22 02:11

    A method group is the name for a set of methods (that might be just one) - i.e. in theory the ToString method may have multiple overloads (plus any extension methods): ToString(), ToString(string format), etc - hence ToString by itself is a "method group".

    It can usually convert a method group to a (typed) delegate by using overload resolution - but not to a string etc; it doesn't make sense.

    Once you add parentheses, again; overload resolution kicks in and you have unambiguously identified a method call.

提交回复
热议问题