3

So i'm not really convinced when its safe to say that a method group conversion occured. We have this multicast delegate from a previous post:

public partial class MainPage : PhoneApplicationPage
{
public delegate void MyDelegate(int a, int b);
// Constructor
public MainPage()
{
    InitializeComponent();

    MyDelegate myDel = new MyDelegate(AddNumbers);
    myDel += new MyDelegate(MultiplyNumbers);
    myDel(10, 20);
}

public void AddNumbers(int x, int y)
{
    int sum = x + y;
    MessageBox.Show(sum.ToString());
}

public void MultiplyNumbers(int x, int y)
{
    int mul = x * y;
    MessageBox.Show(mul.ToString());
}
}

I say that a method group conversion only occurs when we have assigned a method thats overloaded, and at least one overload matches the delegate. In this case there is no method group conversion.

a fellow programmer says that if you don't think MyDelegate myDel = AddNumbers; (with names referring to the question) is a method group conversion, then what would it be then?

The C# Language Specification: An implicit conversion (§6.1) exists from a method group (§7.1) to a compatible delegate type. Given a delegate type D and an expression E that is classified as a method group, an implicit conversion exists from E to D if [...]

So wich point of view is correct?

Freeman
  • 5,691
  • 3
  • 29
  • 41

3 Answers3

4

I say that a method group conversion only occurs when we have assigned a method thats overloaded

Nope, there's no requirement of overloading (by multiple methods) for method group conversions.

Any time you've got just a method name, or a method name qualified with a target (e.g. MultiplyNumbers or foo.MultiplyNumbers), that's a method group. If you're converting that to a delegate instance, that's a method group conversion.

EDIT: The section of the spec which is causing problems is this, in 7.1:

A method group, which is a set of overloaded methods resulting from a member lookup (7.4).

That "set of overloaded methods" can be a set of size 1 - it's still a method group.

This is backed up by 7.6.5.1 (Method Invocations) - emphasis mine:

For a method invocation, the primary-expression of the invocation-expression must be a method group. The method group identifies the one method to invoke or the set of overloaded methods from which to choose a specific method to invoke.

That makes it clear that a method group with one element is a meaningful concept.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I have to admit though that the spec is at least ambiguous here: §7.1: "A method group, which is a set of *overloaded methods* [...]". – Daniel Hilgarth Feb 22 '13 at 13:00
  • @Freeman: On the other hand, in §6.6 there is only one method `F` in the example. – Daniel Hilgarth Feb 22 '13 at 13:03
  • anyway i got the point now. The thing is that 7.1 and 6.6 contradict each other. – Freeman Feb 22 '13 at 13:04
  • @Freeman: I don't really think so - it's just that a set can contain one element. (It would have been really helpful if you'd quoted the bit of the spec that you believed backed you up right from the start.) – Jon Skeet Feb 22 '13 at 13:08
  • @Jon, well ok, that is true, but it is a bit daunting because when someone says a set of overloaded methods you think a set = more then one. – Freeman Feb 22 '13 at 13:10
  • @Freeman: See my edit for an example in the spec (7.6.5.1) which makes this clearer. – Jon Skeet Feb 22 '13 at 13:12
  • Thank you for the effort, that clears it better. As i said when someone says a word that points you to a plural you always think that plural is not singular. Its just basic logic. – Freeman Feb 22 '13 at 13:16
  • But even in english the plural is used to signify an unknown quantity; e.g. "illegally parked cars will be towed" does not suggest there are 2 or more cars. It's also used for zero: "no animals were harmed in the making of this film". Certainly in math an CS it's typical usage to just use plural for elements of sets even when they're usually singletons (sometimes even when they're known to be singletons). – Eamon Nerbonne Feb 22 '13 at 14:04
3

Method group conversion has nothing to do with overloads. Don't get confused by "method group". It doesn't mean that it has to be more than one.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 1
    I don't believe the syntax `new MyDelegate(AddNumbers)` is technically a method group conversion - that's just plain C# 1.0 era delegate creation. The "conversion" refers to the fact that it's using the syntax for conversions, i.e. implicitly - `myDel = AddNumbers`, or explicitly - `myDel = (MyDelegate)AddNumbers`. The distinction is purely terminological, however, as far as I can tell - and explicit conversion seems to me to do exactly the same thing as a creation expression. – Eamon Nerbonne Feb 22 '13 at 13:23
  • @Freeman: Maybe you want to accept Jons answer. It is more detailed. – Daniel Hilgarth Feb 22 '13 at 13:28
  • The expression `AddNumbers` (with nothing more) is classified as a **method group**. When the method group appears inside a `new` expression like `new MyDelegate( ... )`, that entire expression is a _delegate creation expression_ (spec §7.6.10.5). According to the spec, the binding-time processing and the run-time evaluation of a delegate creation expression of this kind is _"in the same way"_ resp. _"as"_ the method group conversion. But a delegate creation expression is not a method group conversion. It only works in an identical manner. – Jeppe Stig Nielsen Feb 22 '13 at 13:28
  • @JeppeStigNielsen you are correct, thank you for helping me to clear the matter. – Freeman Feb 22 '13 at 13:31
  • @DanielHilgarth Ok, will do. – Freeman Feb 22 '13 at 13:31
1

You have assigned a method in which at least one overload matches the delegate - it just so happens there is only one overload. The point of the "conversion" is simply that the compiler can infer the new MyDelegate bit rather than you needing to explicitly construct it.

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166