简体   繁体   English

C# - 委托谓词<T>

[英]C# - delegate Predicate<T>

What is the use of delegate Predicate<T> ,Shall i handle it as nested style like 委托Predicate<T>的用途是什么,我应该像嵌套样式那样处理它

Func<Predicate<T>> ?. Func<Predicate<T>> Simple example please. 简单的例子请。

There are several use cases where its useful to allow the caller to provide a function that returns true or false . 有几种用例允许调用者提供返回truefalse的函数。 The Predicate<T> type is a way to do this. Predicate<T>类型是一种方法。 This is no different than Func<T, bool> (except Predicate<T> was available in earlier versions of C#). 这与Func<T, bool>没有什么不同(除了Predicate<T>在早期版本的C#中可用)。

A simple (and contrived example): 一个简单的(和人为的例子):

string[] strings = new string[] { "hello", "goodbye", "how are you" };

Array.FindAll(strings, delegate(string s)
                           {
                               return s.StartsWith("h");
                           });

or, lambda style: 或者,lambda风格:

Array.FindAll(strings, s => s.StartsWith("h"));

The FindAll method returns an array of all items that match the condition. FindAll方法返回与条件匹配的所有项的数组。 In this case, all the strings that start with "h". 在这种情况下,所有以“h”开头的字符串。 The Predicate is s.StartsWith("h") . Predicates.StartsWith("h")

The useful thing here, is that the Array class knows how to search through all its elements, and create a new array containing only the elements that match. 这里有用的是, Array类知道如何搜索其所有元素,并创建一个只包含匹配元素的新数组。 This part of the algorithm is common to many pieces of code. 算法的这一部分对于许多代码是通用的。 The part that is not common, is the matching criteria, which will vary based on the requirements of the specific piece of code. 不常见的部分是匹配标准,它将根据特定代码段的要求而变化。 Thus the caller specifies that part of the logic, and passes it in as a Predicate . 因此调用者指定逻辑的一部分,并将其作为Predicate传递。

You can handle it in a "nested" style. 可以以“嵌套”样式处理它。 Func<Predicate<T>> means you are declaring a function that returns a Predicate<T> . Func<Predicate<T>>表示您正在声明一个返回Predicate<T>的函数。 Are you sure this is what you want? 你确定这是你想要的吗? What are you trying to achieve? 你想要实现什么目标?

// Make a list of integers.
List<int> list = new List<int>();
list.AddRange(new int[] { 20, 1, 4, 8, 9, 44 });
// Call FindAll() using traditional delegate syntax.
Predicate<int> callback = new Predicate<int>(IsEvenNumber);
List<int> evenNumbers = list.FindAll(callback);

// Target for the Predicate<> delegate.
static bool IsEvenNumber(int i)
{
    // Is it an even number?
    return (i % 2) == 0;
}

This defines a function that returns predicate (a function that returns true or false). 这定义了一个返回谓词的函数(一个返回true或false的函数)。 How could you use this? 你怎么用这个? You can remove the responsibility of which predicate is returned to you by delegating this to another function, who's job is not to return True or False rather to allow you delay the execute of the predicate when you want. 您可以通过将其委托给另一个函数来删除返回给您的谓词的责任,该函数的作业不是返回True或False而是允许您在需要时延迟执行谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM