Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

anonymous delegates in C#

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.

like image 524
Nefzen Avatar asked Jun 10 '09 20:06

Nefzen


People also ask

What are anonymous delegates in C#?

C# - Anonymous Method As the name suggests, an anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type. Example: Anonymous Method.

What is anonymous function in C?

Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function.

What is anonymous method in C sharp?

Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are the methods without a name, just the body. You need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body.

Where are anonymous methods used?

An anonymous method can be used anywhere. A delegate is used and is defined in line, without a method name with the optional parameters and a method body. The scope of the parameters of an anonymous method is the anonymous-method-block. An anonymous method can use generic parameter types like any other method.


1 Answers

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.

like image 147
skb Avatar answered Oct 03 '22 23:10

skb