tags:

views:

1044

answers:

4

Rewritten: All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void?

+5  A: 

Try System.Func<T> and System.Action

JaredPar
I don't think the 0 arg and whatnot exist in .Net 2.0, though.
Brian
It's weird: Func doesn't exist at all in .Net 2.0, though Predicate and Action do.
Joel Coehoorn
A: 

...takes no arguments and has a void return type?

I believe "Action" is a solution to this.

Marcel Lamothe
+3  A: 

All of the Func delegates take at least one parameter

That's not true. They all take at least one type argument, but that argument determines the return type.

So Func<T> accepts no parameters and returns a value. Use Action or Action<T> when you don't want to return a value.

Joel Coehoorn
+8  A: 

All Func delegates return something; all the Action delegates return void.

Func<TResult> takes no arguments and returns TArgument:

public delegate TResult Func<TResult>()

Action<T> takes one argument and does not return a value:

public delegate void Action<T>(T obj)

Action is the simplest, 'bare' delegate:

public delegate void Action()

There's also Func<TArg1, TResult> and Action<TArg1, TArg2> (and others up to four arguments). All of these (except for Action<T>) are new to .NET 3.5 (defined in System.Core).

Jason
FYI, the next version of the base class library will include Func and Action types that support more than four formal parameters. I don't recall exactly how big they get.
Eric Lippert
I hadn't heard that, thanks for heads up Eric!
Marcel Lamothe
In .NET 4.0 they now go up to 8 parameters. If they keep this up, then in the next version it will go up to eleven!!11!!!
michielvoo