Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything. Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value.
A Func is a built-in generic type delegate.
Predicate is the delegate like Func and Action delegates. It represents a method containing a set of criteria and checks whether the passed parameter meets those criteria. A predicate delegate methods must take one input parameter and return a boolean - true or false.
Predicate
: essentially Func<T, bool>
; asks the question "does the specified argument satisfy the condition represented by the delegate?" Used in things like List.FindAll.
Action
: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.
Func
: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.
Other important delegates:
EventHandler
/EventHandler<T>
: Used all over WinForms
Comparison<T>
: Like IComparer<T>
but in delegate form.
Action
, Func
and Predicate
all belong to the delegate family.
Action
: Action can take n input parameters but it returns void.
Func
: Func can take n input parameters but it will always return the result of the provided type. Func<T1,T2,T3,TResult>
, here T1,T2,T3 are input parameters and TResult is the output of it.
Predicate
: Predicate is also a form of Func but it will always return bool. In simple words it is wrapper of Func<T,bool>
.
In addition to Jon's answer, there is also
Converter<TInput, TOutput>
: It's essentially Func<TInput, TOutput>
, but with semantics. Used by List.ConvertAll and Array.ConvertAll, but personally haven't seen it anywhere else.A simple example about the arguments and what retutn each type
This Func take two int arguments and return an int.Func always has return type
Func<int, int, int> sum = (a, b) => a + b;
Console.WriteLine(sum(3, 5));//Print 8
In this case func doesn't have arguments but return a string
Func<string> print = () => "Hello world";
Console.WriteLine(print());//Print Hello world
This Action take two int arguments and return void
Action<int, int> displayInput = (x, y) => Console.WriteLine("First number is :" + x + " , Second number is "+ y);
displayInput(4, 6); //Print First number is :4 , Second number is :6
This Predicate take one argument and always return bool.Generally Predicates always return bool.
Predicate<int> isPositive = (x) => x > 0;
Console.WriteLine(isPositive(5));//Print True
MethodInvoker is one which WinForms developers may use; it accepts no arguments and returns no results. It predates Action, and is still often used when invoking onto the UI thread since BeginInvoke() et al accept an untyped Delegate; although Action will do just as well.
myForm.BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show("Hello, world...");
});
I'd also be aware of ThreadStart and ParameterizedThreadStart; again most people will substitute an Action these days.
Predicate, Func and Action are inbuilt delegate instances of .NET. Each of these delegate instances could refer or point to user methods with specific signature.
Action delegate - Action delegate instances could point to methods that take arguments and returns void.
Func delegate - Func delegate instance could point to method(s) that take variable number of arguments and return some type.
Predicate - Predicates are similar to func delegate instances and they could point to methods that take variable number of arguments and return a bool type.
Action and Func with lambda:
person p = new person();
Action<int, int> mydel = p.add; /*(int a, int b) => { Console.WriteLine(a + b); };*/
Func<string, string> mydel1 = p.conc; /*(string s) => { return "hello" + s; };*/
mydel(2, 3);
string s1= mydel1(" Akhil");
Console.WriteLine(s1);
Console.ReadLine();
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