How do I describe an Action<T> delegate that returns a value (non-void)?

user496949 picture user496949 · Nov 12, 2010 · Viewed 35.9k times · Source

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

Answer

Anthony Pegram picture Anthony Pegram · Nov 12, 2010

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()));