函数、动作、谓词
通过真实的例子及其使用,有人可以帮助我理解:
- 我们什么时候需要
Func
委托? - 我们什么时候需要
Action< ;T>
委托? - 我们什么时候需要
Predicate< ;T>
委托?
With real examples and their use, can someone please help me understand:
- When do we need a
Func<T, ..>
delegate? - When do we need an
Action<T>
delegate? - When do we need a
Predicate<T>
delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Func
和Action
之间的区别只是在于您是否希望委托返回值(使用Func
)或不返回值(使用Action )。
Func
可能在 LINQ 中最常用 - 例如在投影中:或过滤:
或键选择:
Action
更常用于诸如List之类的事情。 .ForEach
:为列表中的每个项目执行给定的操作。我使用它的频率比Func
少,尽管我有时会使用无参数版本来处理Control.BeginInvoke
和Dispatcher.BeginInvoke 之类的事情
。Predicate
实际上只是一个特殊的Func
,在所有Func
和大部分Action< 之前引入/code> 代表们也来了。我怀疑,如果我们已经有了各种形式的
Func
和Action
,就不会引入Predicate
...尽管它确实为委托的使用赋予了一定的含义,而Func
和Action
则用于广泛不同的目的。Predicate
主要用于List
中,用于FindAll
和RemoveAll
等方法。The difference between
Func
andAction
is simply whether you want the delegate to return a value (useFunc
) or not (useAction
).Func
is probably most commonly used in LINQ - for example in projections:or filtering:
or key selection:
Action
is more commonly used for things likeList<T>.ForEach
: execute the given action for each item in the list. I use this less often thanFunc
, although I do sometimes use the parameterless version for things likeControl.BeginInvoke
andDispatcher.BeginInvoke
.Predicate
is just a special casedFunc<T, bool>
really, introduced before all of theFunc
and most of theAction
delegates came along. I suspect that if we'd already hadFunc
andAction
in their various guises,Predicate
wouldn't have been introduced... although it does impart a certain meaning to the use of the delegate, whereasFunc
andAction
are used for widely disparate purposes.Predicate
is mostly used inList<T>
for methods likeFindAll
andRemoveAll
.Action 是一个方法的委托(指针),它接受零个、一个或多个输入参数,但不返回任何内容。
Func 是一个方法的委托(指针),它接受零个、一个或多个输入参数,并返回一个值(或引用)。
Predicate 是一种特殊的 Func,通常用于比较(采用通用参数并返回 bool)。
尽管 Action 和 Func 广泛与 Linq 一起使用,但它们在逻辑上是独立于 Linq 的概念。 C++ 已经包含了类型化函数指针形式的基本概念。
下面是一个不使用 Linq 的 Action 和 Func 的小示例:
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).
Predicate is a special kind of Func often used for comparisons (takes a generic parameter and returns bool).
Though widely used with Linq, Action and Func are concepts logically independent of Linq. C++ already contained the basic concept in form of typed function pointers.
Here is a small example for Action and Func without using Linq:
Func - 当您想要一个函数的委托时,该函数可能会也可能不会接受参数并返回值。最常见的示例是从 LINQ 中选择:
操作 - 当您想要一个可能接受或不接受参数且不返回值的函数的委托时。我经常将这些用于匿名事件处理程序:
Predicate - 当您想要一个专门版本的 Func 来根据一组条件评估值并返回布尔结果(匹配时为 true,否则为 false) 。
同样,这些在 LINQ 中非常频繁地用于诸如Where之类的事情:我刚刚仔细检查过,结果发现 LINQ 不使用谓词。不知道他们为什么做出这个决定……但从理论上讲,这仍然是谓词适合的情况。
Func - When you want a delegate for a function that may or may not take parameters and returns a value. The most common example would be Select from LINQ:
Action - When you want a delegate for a function that may or may not take parameters and does not return a value. I use these often for anonymous event handlers:
Predicate - When you want a specialized version of a Func that evaluates a value against a set of criteria and returns a boolean result (true for a match, false otherwise).
Again, these are used in LINQ quite frequently for things like Where:I just double checked and it turns out that LINQ doesn't use Predicates. Not sure why they made that decision...but theoretically it is still a situation where a Predicate would fit.