代表:谓语vs.动作vs.功能 [英] Delegates: Predicate vs. Action vs. Func
问题描述
有人可以对这3个最重要的代表提供很好的解释(希望有例子)。
Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates:
- 谓词
- 动作
- 功能
推荐答案
-
谓词
:本质上是Func< T,bool>
;提出问题指定的参数是否满足委托代表的条件?在类似List.FindAll的东西中使用。Predicate
: essentiallyFunc<T, bool>
; asks the question "does the specified argument satisfy the condition represented by the delegate?" Used in things like List.FindAll.Action
:在给定参数的情况下执行操作。非常通用。Action
: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.Func
:在LINQ中未使用太多,基本上就意味着副作用。在LINQ中>广泛地,通常用于转换参数,例如Func
: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.其他重要代表:
-
EventHandler
/EventHandler< T>
:全部使用在WinForms上
EventHandler
/EventHandler<T>
: Used all over WinForms
Comparison< T>
:就像IComparer< T>
,但采用代理人形式。Comparison<T>
: LikeIComparer<T>
but in delegate form.这篇关于代表:谓语vs.动作vs.功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-