views:

331

answers:

3

I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is:

Why do:

Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod));

When you can do:

Thread t = new Thread(SomeObject.SomeMethod);

Both will compile and work in my experience...am I missing something?

+5  A: 

As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is because ThreadStart is defined as a delegate that returns void and takes no parameters and therefore there is an implicit conversion from the method group SomeObject.SomeMethod to ThreadStart. Thus, both are invoking the overload Thread(ThreadStart) of the Thread constructor .

The relevant section of the language specification is §6.6 (Method group conversions).

I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference?

So, just a correction of terminology here. With

class MyObject {
    public void SomeMethod() { }
}

MyObject someObject = new MyObject();

the thing denoted by someObject.SomeMethod is a method group. You can just think of it as the set of overloaded methods can that be looked up using the notation someObject.SomeMethod.

Jason
Is `someObject.SomeMethod()` also a method group?
John Feminella
@John Feminella: No, that would be a method invocation.
Jason
Hmm, okay. What if `SomeMethod` had several overloads whose signature matched `SomeMethod(...)`, where `...` are arbitrary parameters? Is it a method group then, since it could refer to one of several methods and has not yet been resolved?
John Feminella
@John Feminella: Sorry, your question is not clear to me. Are you asking about an invocation expression. For example, `someObject.SomeMethod(parameter1, parameter2, ..., parametern)` where each of `parameterj` is an expression, `ref variable reference` or `out variable reference`? For such an expression, there is either a best applicable overload (§7.4.3 of the specification), an attempt is made to process the expression as an extension method invocation or a compile-time error occurs; to be clear: ambiguous invocations are compile-time errors. Is that what you're asking about?
Jason
I may be misunderstanding what a method group is. I'm going off your last sentence, which says it's "the set of overloaded methods that can be looked up using the notation ...". If I write `SomeMethod(a, b)` and there are several overloads that could match these arguments (even if one is eventually resolved to be the right one through the overload resolution rules), is that a method group?
John Feminella
@John Feminella: No that is not a method group, that is a method invocation. Notice that in my sentence that you refer to there are no parentheses etc. in the expression `someObject.SomeMethod`. Once parentheses and parameters are thrown in the expression either becomes a legal invocation expression or a compile-time error occurs.
Jason
A: 

That's equivalent. Good introductory article on the subject: C# Delegates, Anonymous Methods, and Lambda Expressions – O My!

herzmeister der welten
+2  A: 

The compiler will infer that when you typed the shorter code, you meant the longer code. There's no difference in the ultimate effect. If you want the clarity of the full constructor, you can put it in; if you want the brevity of just the method group, you can allow the compiler to infer the constructor. It's just a stylistic choice.

Eric Lippert