How do I describe an Action delegate that retur

2019-03-09 15:58发布

问题:

The Action<T> delegate return void. Is there any other built-in delegate which returns non void value?

回答1:

Yes. Func<> returns the type specified as the final generic type parameter, such that Func<int> returns an int and Func<int, string> accepts an integer and returns a string. Examples:

Func<int> getOne = () => 1;
Func<int, string> convertIntToString = i => i.ToString();
Action<string> printToScreen = s => Console.WriteLine(s);
// use them

printToScreen(convertIntToString(getOne()));


回答2:

Sure, the Func Delegates return T.

Func<TResult> is "TResult method()"
Func<TInput, TResult> is "TResult method(TInput param)"

All the way down to

Func<T1, T2, T3, T4, TResult>

http://msdn.microsoft.com/en-us/library/bb534960.aspx

http://msdn.microsoft.com/en-us/library/bb534303.aspx

Also, for the sake of completeness, there is Predicate which returns bool.

Predicate<T> is "bool method(T param)"

http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx