What is a method group in C#?

C#.NetMethod Group

C# Problem Overview


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

var list = new List<string>();
// ... snip
list.Add(someObject.ToString);

of course there was a typo in the last line because I forgot the invocation parentheses after ToString. The correct form would be:

var list = new List<string>();
// ... snip
list.Add(someObject.ToString()); // <- notice the parentheses

However I came to wonder what is a method group. Google isn't much of a help nor MSDN.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

Also, if you are using LINQ, you can apparently do something like myList.Select(methodGroup).

So, for example, I have:

private string DoSomethingToMyString(string input)
{
    // blah
}

Instead of explicitly stating the variable to be used like this:

public List<string> GetStringStuff()
{
    return something.getStringsFromSomewhere.Select(str => DoSomethingToMyString(str));
}

I can just omit the name of the var:

public List<string> GetStringStuff()
{
    return something.getStringsFromSomewhere.Select(DoSomethingToMyString);
}

Solution 3 - C#

You can cast a method group into a delegate.

The delegate signature selects 1 method out of the group.

This example picks the ToString() overload which takes a string parameter:

Func<string,string> fn = 123.ToString;
Console.WriteLine(fn("00000000"));

This example picks the ToString() overload which takes no parameters:

Func<string> fn = 123.ToString;
Console.WriteLine(fn);

Solution 4 - C#

The first result in your MSDN search said:

> The method group identifies the one > method to invoke or the set of > overloaded methods from which to > choose a specific method to invoke

my understanding is that basically because when you just write someInteger.ToString, it may refer to:

Int32.ToString(IFormatProvider) 

or it can refer to:

Int32.ToString()

so it is called a method group.

Solution 5 - C#

The ToString function has many overloads - the method group would be the group consisting of all the different overloads for that function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrei R&#238;neaView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#KaelesView Answer on Stackoverflow
Solution 3 - C#JackView Answer on Stackoverflow
Solution 4 - C#oscarkuoView Answer on Stackoverflow
Solution 5 - C#1800 INFORMATIONView Answer on Stackoverflow