Delegates: Predicate Action Func

2019-01-04 16:28发布

Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates:

  • Predicate
  • Action
  • Func

What other delegates should a C# developer be aware of?

How often do you use them in production code?

标签: c# delegates
8条回答
劳资没心,怎么记你
2楼-- · 2019-01-04 16:47

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-04 16:52

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>.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-04 16:53

Func is more LINQ friendly, can be passed in as a parameter. (point-free)

Predicate cannot, has to be wrapped again.

Predicate<int> IsPositivePred = i => i > 0;
Func<int,bool> IsPositiveFunc = i => i > 0;

new []{2,-4}.Where(i=>IsPositivePred(i)); //Wrap again

new []{2,-4}.Where(IsPositivePred);  //Compile Error
new []{2,-4}.Where(IsPositiveFunc);  //Func as Parameter
查看更多
唯我独甜
5楼-- · 2019-01-04 16:56
  • 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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-04 16:58

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.

查看更多
看我几分像从前
7楼-- · 2019-01-04 16:58

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();
查看更多
登录 后发表回答