34

I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code:

private void RefreshForm()
{
    if (InvokeRequired)
        Invoke(new InvokeDelegate(Refresh));
    else
        Refresh();
}

I'm not even sure I have to, I just read enough to be scared that it won't work at some later stage.
InvokeDelegate is actually declared in another file, but do I really need an entire delegate dedicated just for this? aren't there any generic delegates at all?
I mean, for example, there's a Pen class, but there's also Pens.pen-of-choice so you don't have to remake the whole thing. It's not the same, but I hope you understand what I mean.

Lucas
  • 17,277
  • 5
  • 45
  • 40
Nefzen
  • 7,819
  • 14
  • 36
  • 34
  • 1
    There's no such thing as "anonymous delegates" in C#. There are delegates (like what you are using) and anonymous methods (which are only accessible through a delegate). – Lucas Jun 10 '09 at 21:37
  • 3
    I guess since the C# spec uses the term "anonymous method" that it would be the correct way. However, this term is used by a lot of people to mean a delegate instantiated with an anonymous method as a parameter. So, they mean the same thing, but the context in which someone might use one term over the other depends often on whether or not there is a delegate assignment occurring or if the anonymous method is being passed directly to another method. I welcome any arguments to the contrary. – Richard Anthony Hein Jun 10 '09 at 23:17

6 Answers6

52

Yes. In .NET 3.5 you can use Func and Action delegates. The Func delegates return a value, while Action delegates return void. Here is what the type names would look like:

System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)

System.Action // (no arg, no return value)
System.Action<T> // (1 arg, no return value)
System.Action<T1, T2> // (2 arg, no return value)
System.Action<T1, T2, T3> // (3 arg, no return value)
System.Action<T1, T2, T3, T4> // (4 arg, no return value)

I don't know why they stopped at 4 args each, but it has always been enough for me.

Daniel
  • 10,864
  • 22
  • 84
  • 115
skb
  • 30,624
  • 33
  • 94
  • 146
26

There's the Action delegate you could use, like so:

private void RefreshForm()
{
    if (InvokeRequired) Invoke(new Action(Refresh));
    else Refresh();
}

Or, with lambda syntax:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(() => Refresh()));
    else Refresh();
}

Finally there's anonymous delegate syntax:

private void RefreshForm()
{
    if (InvokeRequired) Invoke((Action)(delegate { Refresh(); }));
    else Refresh();
}
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
8

In this specific case you can (and should) just use MethodInvoker to do this... that is why it exists.

if (InvokeRequired)
    Invoke(new MethodInvoker(Refresh));
else
    Refresh();

If you were doing something else you could, as others have answered use Func<T,...> or Action<T,...> if they fit your use case.

Brian ONeil
  • 4,229
  • 2
  • 23
  • 25
5

Short version:

Invoke((MethodInvoker)delegate { Refresh(); });

Then you can also drop the check of InvokeRequired; you can just call it as it is. Works also if you need to pass parameters, so there is no need for other parameter-specific delegates (works just as well with the parameter-less Action delegate as well):

private void SetControlText(Control ctl, string text)
{
    Invoke((MethodInvoker)delegate { ctl.Text = text; });
}
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Why would you create an anonymous delegate just to cast it to an existing delegate type? You should just use the delegate type directly. – Brian ONeil Jun 10 '09 at 20:59
  • 2
    @Brian: you mean like this: Invoke(new MethodInvoker(delegate { ctl.Text = text; })); ? As far as I can see that produces the exact same IL code as my code above, so it's down to personal taste, I guess. – Fredrik Mörk Jun 10 '09 at 21:07
  • what is the difference between MethodInvoker and Action? Also, I am checking InvokeRequired because it seems a better practice, eg, faster. – Nefzen Jun 10 '09 at 21:10
  • @Fredrik: Brian meant using new MethodInvokder(Refresh) rather than (MethodInvoker)delegate { Refresh(); } – Nefzen Jun 10 '09 at 21:15
  • if it is the same IL in the end, then I guess it is more about preference, but if the delegate exists I don't understand why you would prefer to use the anonymous syntax. Just seems more straight forward to just declare it and use it. – Brian ONeil Jun 10 '09 at 21:21
  • 1
    Use Invoke((MethodInvoker)Refresh) or Invoke(new MethodInvoker(Refresh)) instead of Invoke((MethodInvoker)delegate { Refresh(); }) ... the latter creates an additional level of indirection by creating an anonymous method which only calls Refresh(), instead of creating a delegate to Refresh() itself. – Lucas Jun 10 '09 at 21:39
  • @Purusartha: That sounds strange. Can you provide the code that reproduces that? Would be interesting to see so that I can correct any errors. – Fredrik Mörk Mar 22 '11 at 08:30
2

Do I really need an entire delegate dedicated just for this? aren't there any generic delegates at all?

Defining your own delegates can really make debugging easier, if only because Intellisense can tell you the names of your parameters. For example, you write a delegate like this:

public delegate int UpdateDelegate(int userID, string city, string, state, string zip);

When you use it code, .NET will inform you of the parameter names, delegate name, etc, so there's a lot of context right in the delegate definition if you aren't sure exactly how something is used.

However, if you don't mind sacrificing Intellisense, there is already a class of delegates definined in the System namespace which can be used as ad-hoc delegates:

Func<T>
Func<T, U>
Func<T, U, V>
Func<T, U, V, W>
Action, Action<T>
Action<T, U>
Action<T, U, V>
Action<T, U, V, W>

Only Action and Action exist in .NET 2.0, but its easy enough to declare a helper class with the remaining delegates you need for these kind of miscellaneous ad hoc functions.

Juliet
  • 80,494
  • 45
  • 196
  • 228
1

Yes, there are generic delegates. Action<T1, T2...> is a generic delegate that takes some parameters and returns no value, and Func<T1, T2...R> is a generic delegate that takes some parameters and returns a value.

mqp
  • 70,359
  • 14
  • 95
  • 123