Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Groups (C# In Depth) - Need help understanding better what a Method Group is

Tags:

c#

So I have read a number of StackOverflow questions related to "what is a method group" as well as other internet articles, they all say the same thing in the bottom line - that a method group is "a group of overloaded methods".

However, reading Jon Skeet's "C# In Depth (second edition)", he states the following verbiage regarding method group in the context of Lambda expressions (chapter 9.4.1)

Reasons for change: streamlining generic method calls

Type inference occurs in a few situations. We’ve already seen it apply to implicitly typed arrays, and it’s also required when you try to implicitly convert a method group to a delegate type. This can be particularly confusing when the conversion occurs when you’re using a method group as an argument to another method: with overloading of the method being called, and overloading of methods within the method group, and the possibility of generic methods getting involved, the set of potential conversions may be enormous."

Either method groups are allot more than just a group of overloaded methods, or he saying you can actually create a delegate that retains an entire method group. Or something entirely different that I am not quite grasping.

Can someone explain what he is stating is possible here?

Thanks,

like image 655
pghtech Avatar asked Jan 21 '11 15:01

pghtech


2 Answers

The scenario that Jon is describing is this one:

int M() {...}
string M<T>(T t) {...}
double M<T>(List<T> t) {...} 
static void M(double d) {...}
... etc ...

void N(Func<int> f) {...}
void N<T>(Action<T> a) {...}
void N<T>(Func<IEnumerable<T>> f) {...}
... etc ...

N(M);

N and M are both method groups, containing potentially dozens of methods, some that might be generic. The problem posed to the compiler is "work out which methods N and M are the ones that the developer intended, and work out the type arguments if the intended method is generic".

To work all that out the compiler has to try every possible N, and then figure out which of every possible M is compatible with the delegate type of the formal parameter of that overload of N. It has to discard the ones that don't work, and then from all those potentially hundreds or thousands of combinations left, figure out which one is the "best", if any. This is a somewhat tricky problem in semantic analysis.

like image 194
Eric Lippert Avatar answered Nov 07 '22 09:11

Eric Lippert


Method group is an expression representing set of overloaded methods, thus it should be obvious that he is saying about creating a delegate using such expression. E.g.:


class Foo
{
public static void M() {}
public static void M(int _) {}
}
...
Action a = new Action(Foo.M); // Foo.M is a method group here.

You can also take a look at the C# spec for more detailed info.

An expression is classified as one of the following:
...
• A method group, which is a set of overloaded methods resulting from a member lookup (§7.4). A method group may have an associated instance expression and an associated type argument list. When an instance method is invoked, the result of evaluating the instance expression becomes the instance represented by this (§7.6.7). A method group is permitted in an invocation-expression (§7.6.5) , a delegate-creation-expression (§7.6.10.5) and as the left hand side of an is operator, and can be implicitly converted to a compatible delegate type (§6.6). In any other context, an expression classified as a method group causes a compile-time error.
like image 29
Konstantin Oznobihin Avatar answered Nov 07 '22 09:11

Konstantin Oznobihin