有人可以对这3个最重要的代表提供很好的解释(希望有例子):>
有人可以对这3个最重要的代表提供很好的解释(希望有一些例子:谓词操作函数)>
Predicate
:本质上是Func<T, bool>
;提出问题“指定的参数是否满足委托代表的条件?”用于List.FindAll之类的东西。
Action
,Func
和Predicate
都属于委托家族。Action
:动作可以接受n个输入参数,但返回空值。
Converter<TInput, TOutput>
:本质上是Func<TInput, TOutput>
,但具有语义。由List.ConvertAll和Array.ConvertAll使用,但个人在其他任何地方都没有看到它。myForm.BeginInvoke((MethodInvoker)delegate
{
MessageBox.Show("Hello, world...");
});
动作委托-动作委托实例可以指向带有参数并返回void的方法。
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();
谓词不能,必须重新包装。
此Func接受两个int参数并返回int。Func始终具有返回类型