Explicit vs implicit delegate creation?

  • Thread starter Thread starter Peter Duniho
  • Start date Start date
P

Peter Duniho

I was reminded in a recent post
(http://groups.google.com/group/microsoft.public.dotnet.framework/msg/e649e992db857691?dmode=source)
that in C# one can use anonymous delegates without explicitly creating a
new delegate instance. However, I still run into situations in which the
compiler complains.

For example, I get a compiler error here:

Invoke(delegate() { /* some code */ });

which is fixable like this:

Invoke(new MethodInvoker(delegate() { /* some code */ }));

What gives? Is this a compiler version difference? Something about the
specific context? In what cases are you actually allowed to not contain
the anonymous delegate in a constructor for an actual delegate type, and
why doesn't that apply in the above example?

Pete
 
Peter said:
I was reminded in a recent post
<removed>
that in C# one can use anonymous delegates without explicitly creating a
new delegate instance. However, I still run into situations in which the
compiler complains.

For example, I get a compiler error here:

Invoke(delegate() { /* some code */ });

which is fixable like this:

Invoke(new MethodInvoker(delegate() { /* some code */ }));

What gives? Is this a compiler version difference? Something about the
specific context? In what cases are you actually allowed to not contain
the anonymous delegate in a constructor for an actual delegate type, and
why doesn't that apply in the above example?

Pete

Hi Pete,

What's the compiler error you get? Using gmcs, the following code for me
compiles and executes fine:

///
using System;

public class E
{
public delegate void VoidDeleg ();

public static void Main ()
{
Invoke(delegate() { Console.WriteLine("Foobar"); });
}

public static void Invoke ( VoidDeleg deleg )
{
deleg();
}
}
///
 
What's the compiler error you get?

I get:

Error 1 The best overloaded method match for
'System.Windows.Forms.Control.Invoke(System.Delegate)' has some invalid
arguments
Error 2 Argument '1': cannot convert from 'anonymous method' to
'System.Delegate'

However, now that I've had a moment to think about it (and of course, have
committed my ignorance to posterity :) ), I think I might understand the
issue.

The Control.Invoke() method doesn't have a specific delegate type as the
parameter, even in the single parameter case. It uses the abstract
System.Delegate type, and so I can see how the compiler cannot implicitly
convert the anonymous delegate to that type. When given a concrete type,
the compiler can.

Of course, that begs the question why, in the case of single parameter
Control.Invoke() method, did they use the abstract type? Since no
parameters can be passed, the MethodInvoker type would have worked just
fine, I think.

Anyway, while I think I've answered my own question, if there is
additional insight to be had here, I welcome any other responses.

Thanks!
Pete
 
Peter said:
I get:

Error 1 The best overloaded method match for
'System.Windows.Forms.Control.Invoke(System.Delegate)' has some invalid
arguments
Error 2 Argument '1': cannot convert from 'anonymous method' to
'System.Delegate'

However, now that I've had a moment to think about it (and of course, have
committed my ignorance to posterity :) ), I think I might understand the
issue.

The Control.Invoke() method doesn't have a specific delegate type as the
parameter, even in the single parameter case. It uses the abstract
System.Delegate type, and so I can see how the compiler cannot implicitly
convert the anonymous delegate to that type. When given a concrete type,
the compiler can.

Of course, that begs the question why, in the case of single parameter
Control.Invoke() method, did they use the abstract type? Since no
parameters can be passed, the MethodInvoker type would have worked just
fine, I think.

Anyway, while I think I've answered my own question, if there is
additional insight to be had here, I welcome any other responses.

Thanks!
Pete

Hi Pete,

You're spot on. In my example I made my own 'Invoke' method with a concrete
type, and of course, the Invoke method accepts the abstract type
System.Delegate.
Of course, that begs the question why, in the case of single parameter
Control.Invoke() method, did they use the abstract type? Since no
parameters can be passed, the MethodInvoker type would have worked just
fine, I think.

Well, one of the overloads accepts a parameter list, and changing the single
overload to a different type other than delegate (as it appears in the
overload) would reduce consistency.

Probably.
 
Back
Top