The Action<T>
delegate return void. Is there any other built-in delegate which returns non void value?
If you have a method that accepts two numbers and you want to add them and return the sum of the two numbers, you can use a delegate to store the return value of the method as shown in the code snippet given below. int result = d(12, 15);
Action delegate is an in-built generic type delegate. This delegate saves you from defining a custom delegate as shown in the below examples and make your program more readable and optimized. It is defined under System namespace.
public delegate void Del(string message); A delegate object is normally constructed by providing the name of the method the delegate will wrap, or with a lambda expression. Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.
The myDelegate is a user-defined name of the delegate that takes a single integer parameter and its return type is void that does not return any value.
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()));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With