147

有人可以为这三个最重要的代表提供一个很好的解释(希望有例子):

  • 谓词
  • 行动
  • 功能
4

8 回答 8

191
  • Predicate: 本质上Func<T, bool>; 提出问题“指定的参数是否满足委托所代表的条件?” 用于 List.FindAll 之类的东西。

  • Action: 执行给定参数的动作。非常通用的用途。在 LINQ 中使用不多,因为它基本上意味着副作用。

  • Func:在 LINQ 中广泛使用,通常用于转换参数,例如通过将复杂结构投影到一个属性。

其他重要代表:

  • EventHandler/ EventHandler<T>: 在 WinForms 中使用

  • Comparison<T>: 类似IComparer<T>,但以委托形式。

于 2009-02-19T21:22:43.643 回答
64

ActionFuncPredicate属于委托家族。

Action:动作可以接受 n 个输入参数,但它返回 void。

Func: Func 可以接受 n 个输入参数,但它总是返回所提供类型的结果。Func<T1,T2,T3,TResult>, 这里 T1,T2,T3 是输入参数,TResult 是它的输出。

Predicate: Predicate 也是 Func 的一种形式,但它总是返回 bool。简单来说,它是Func<T,bool>.

于 2015-05-14T10:08:05.327 回答
10

除了乔恩的回答,还有

  • Converter<TInput, TOutput>: 本质上是Func<TInput, TOutput>,但有语义。由 List.ConvertAll 和 Array.ConvertAll 使用,但个人在其他任何地方都没有见过。
于 2010-01-04T23:02:38.220 回答
8

关于参数的简单示例以及每种类型的返回值

此 Func 接受两个 int 参数并返回一个 int.Func 始终具有返回类型

 Func<int, int, int> sum = (a, b) => a + b;
 Console.WriteLine(sum(3, 5));//Print 8

在这种情况下 func 没有参数但返回一个字符串

Func<string> print = () => "Hello world";
Console.WriteLine(print());//Print Hello world

此操作接受两个 int 参数并返回 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

此谓词采用一个参数并始终返回 bool。通常谓词始终返回 bool。

Predicate<int> isPositive = (x) => x > 0;
Console.WriteLine(isPositive(5));//Print True
于 2019-05-28T08:54:36.797 回答
4

MethodInvoker 是 WinForms 开发人员可以使用的一种;它不接受任何参数并且不返回任何结果。它早于 Action,并且在调用 UI 线程时仍然经常使用,因为 BeginInvoke() 等人接受无类型的委托;尽管 Action 也可以。

myForm.BeginInvoke((MethodInvoker)delegate
{
  MessageBox.Show("Hello, world...");
});

我也知道 ThreadStart 和 ParameterizedThreadStart;这些天大多数人都会再次替换 Action。

于 2011-03-29T11:52:34.353 回答
3

Predicate、Func 和 Action 是 .NET 的内置委托实例。这些委托实例中的每一个都可以引用或指向具有特定签名的用户方法。

动作委托 - 动作委托实例可以指向接受参数并返回 void 的方法。

Func 委托 - Func 委托实例可以指向采用可变数量参数并返回某种类型的方法。

谓词 - 谓词类似于 func 委托实例,它们可以指向采用可变数量参数并返回 bool 类型的方法。

于 2012-03-11T19:09:02.680 回答
2

带有 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();
于 2017-05-06T10:24:33.123 回答
2

Func 对 LINQ 更友好,可以作为参数传入。(无积分)

谓词不能,必须再次包装。

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
于 2018-07-13T20:39:28.273 回答