Skip to content

What is method group?

George George

Asked Sep 10, 2008, 2:44 AM EDT2 answers
Share
LinkedInX

Hello everyone,


After quite some time study on samples and C# Spec. I am confused about what is a method group? In my understanding method group is a set of methods which have the same signature, and not necessary to belong to a specific class. But I am not sure whether I am correct. Any comments or ideas?

Below is what I found in my study.

From ECMA-334: 14.1,

http://en.csharp-online.net/ECMA-334:_14.1_Expression_classifications

A method group, which is a set of overloaded methods resulting from a member lookup (§14.3). A method group can have an associated instance expression. When an instance method is invoked, the result of evaluating the instance expression becomes the instance represented by this (§14.5.7). A method group can be used in an invocation-expression (§14.5.5), used in a delegate-creation-expression (§14.5.10.3), or implicitly converted to a compatible delegate type. In any other context, an expression classified as a method group causes a compile-time error.

From C# in Depth,

http://csharpindepth.com/ViewNote.aspx?NoteID=92

"the feature allows you to specify an extension method as a method group using extension syntax"


thanks in advance,
George

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Jon Skeet
    +1
    Your understanding isn't quite correct. From the C# 3 spec, section 7.1: "A method group, which is a set of overloaded methods resulting from a member lookup (§7.3). A method group may have an associated instance expression and an associated type argument list." So in other words, when you write something like: EventHandler handler = MyMethod; then "MyMethod" refers to a method group. There could be multiple methods with the same name, but different signatures. The method group conversion creates a delegate calling the appropriate actual method. All the methods in the group are available in the same context - they could belong to different classes due to inheritance, or there could be extension methods available - but they're related in terms of the context the member lookup is occurring in. Does that help? Jon (PS: Apologies for the poor formatting. I'm not sure what it is about this forum, but it doesn't seem to be respecting line breaks...)
    +1
  • +1

    Thanks Jon,

     

    Your answer is quite clear. :-)

    Let me confirm, you mean method groupd are a group of methods which are all the candidates when we make a method call. But actually which method in the candidate methods will be called is dependent on the method look-up priority and sequence. Is that correct understanding?

    My current confusion is I am not sure whether it should be all the methods with the same name be called method group -- or only the candidate methods which ar eligible to be called according to C# syntax and runtime parameter type checking/conversion are called method group.

     

    regards,

    George

    +1