How do I describe an Action delegate that retur

2019-03-09 16:10发布

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

2条回答
闹够了就滚
2楼-- · 2019-03-09 16:34

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()));
查看更多
不美不萌又怎样
3楼-- · 2019-03-09 16:42

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

查看更多
登录 后发表回答