Func vs. Action vs. Predicate [复制]
本文翻译自:Func vs. Action vs. Predicate [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- Delegates: Predicate Action Func 8 answers 代表:Predicate Action Func 8个答案
With real examples and their use, can someone please help me understand: 通过实例和它们的使用,有人可以帮助我理解:
- When do we need Func delegate? 我们什么时候需要Func委托?
- When do we need Action delegate? 我们什么时候需要行动委托?
- When do we need Predicates delegate? 我们什么时候需要Predicates委托?
#1楼
参考:https://stackoom.com/question/I7Al/Func-vs-Action-vs-Predicate-复制
#2楼
Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything. Action是方法的委托(指针),它接受零个,一个或多个输入参数,但不返回任何内容。
Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference). Func是方法的委托(指针),它接受零个,一个或多个输入参数,并返回一个值(或引用)。
Predicate is a special kind of Func often used for comparisons. 谓词是一种特殊的Func,经常用于比较。
Though widely used with Linq, Action and Func are concepts logically independent of Linq. 虽然广泛用于Linq,但Action和Func在概念上与Linq无关。 C++ already contained the basic concept in form of typed function pointers. C ++已经包含了类型化函数指针形式的基本概念。
Here is a small example for Action and Func without using Linq: 这是Action和Func的一个小例子,不使用Linq:
class Program
{static void Main(string[] args){Action<int> myAction = new Action<int>(DoSomething);myAction(123); // Prints out "123"// can be also called as myAction.Invoke(123);Func<int, double> myFunc = new Func<int, double>(CalculateSomething);Console.WriteLine(myFunc(5)); // Prints out "2.5"}static void DoSomething(int i){Console.WriteLine(i);}static double CalculateSomething(int i){return (double)i/2;}
}
#3楼
The difference between Func
and Action
is simply whether you want the delegate to return a value (use Func
) or not (use Action
). Func
和Action
之间的区别仅在于您是否希望委托返回值(使用Func
)或不使用(使用Action
)。
Func
is probably most commonly used in LINQ - for example in projections: Func
可能是LINQ中最常用的 - 例如在投影中:
list.Select(x => x.SomeProperty)
or filtering: 或过滤:
list.Where(x => x.SomeValue == someOtherValue)
or key selection: 或关键选择:
list.Join(otherList, x => x.FirstKey, y => y.SecondKey, ...)
Action
is more commonly used for things like List<T>.ForEach
: execute the given action for each item in the list. Action
更常用于List<T>.ForEach
:对列表中的每个项执行给定的操作。 I use this less often than Func
, although I do sometimes use the parameterless version for things like Control.BeginInvoke
and Dispatcher.BeginInvoke
. 我用这个少往往比Func
,虽然我有时会使用,例如,无参数的版本Control.BeginInvoke
和Dispatcher.BeginInvoke
。
Predicate
is just a special cased Func<T, bool>
really, introduced before all of the Func
and most of the Action
delegates came along. Predicate
只是一个特殊的套装Func<T, bool>
真的,在所有Func
和大多数Action
代表出现之前引入。 I suspect that if we'd already had Func
and Action
in their various guises, Predicate
wouldn't have been introduced... although it does impart a certain meaning to the use of the delegate, whereas Func
and Action
are used for widely disparate purposes. 我怀疑,如果我们已经有各种伪装的Func
和Action
,那么Predicate
就不会被引入......尽管它确实赋予了代表使用某种意义,而Func
和Action
则被广泛地使用了目的。
Predicate
is mostly used in List<T>
for methods like FindAll
and RemoveAll
. Predicate
主要用于List<T>
用于FindAll
和RemoveAll
等方法。
#4楼
Func - When you want a delegate for a function that may or may not take parameters and returns a value. Func - 当您想要一个函数的委托时,该函数可能会也可能不会获取参数并返回一个值。 The most common example would be Select from LINQ: 最常见的例子是从LINQ中选择:
var result = someCollection.Select( x => new { x.Name, x.Address });
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: 我经常将它们用于匿名事件处理程序:
button1.Click += (sender, e) => { /* Do Some Work */ }
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). 谓词 - 当你想要一个特殊版本的Func,它根据一组条件评估一个值并返回一个布尔结果(匹配为true,否则为false)。 Again, these are used in LINQ quite frequently for things like Where: 同样,这些在LINQ中经常用于像Where这样的东西:
var filteredResults = someCollection.Where(x => x.someCriteriaHolder == someCriteria);
I just double checked and it turns out that LINQ doesn't use Predicates. 我只是仔细检查,结果是LINQ不使用Predicates。 Not sure why they made that decision...but theoretically it is still a situation where a Predicate would fit. 不确定他们为什么做出这个决定......但理论上它仍然是谓词适合的情况。
Func vs. Action vs. Predicate [复制]相关推荐
- 第十节:委托和事件(2)(泛型委托、Func和Action、事件及与委托的比较)
一. 泛型委托 所谓的泛型委托,即自定义委托的参数可以用泛型约束,同时内置委托Func和Action本身就是泛型委托. 将上一个章节中的Calculator类中的方法用自定义泛型委托重新实现一下. 1 ...
- delegate,event, lambda,Func,Action以及Predicate
delegate 委托 可以理解为c++ 语言中的函数指针,标示了方法调用的回调函数的规范.强类型,便于编译时检查是它的最大优点,从此可以和void * 说再见了. event 事件 用以delega ...
- [C#基础]Func和Action学习
目录 委托 Action Func 总结 委托 委托的那些事 关于委托的基本定义,在很久之前的这篇文章中,有个简单的介绍.稍微回顾一下. 委托是c#中类型安全的,可以订阅一个或多个具有相同签名方法的函 ...
- Func与Action
平时我们如果要用到委托一般都是先声明一个委托类型,比如: private delegate string Say(); string说明适用于这个委托的方法的返回类型是string类型,委托名Say后 ...
- 什么是LambdaExpression,如何转换成Func或Action(2)
序言 在上一篇中,我们认识了什么是表达式树.什么是委托,以及它们的关系.可能是我功力不好,貌似大家都不怎么关注,没有讲解出不同角度的问题. 学习一种新技术,是枯燥的过程,只有在你掌握后并能运用时才能从 ...
- Func 与Action
Func< > 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法, Action<T> 委托封装一个方法,该方法只有一个参数并且不返回值. 其实都是一个委 ...
- Func和Action的介绍及其用法
Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如在反射中使用就可以弥补反射所损失的性能. ...
- 《ASP.NET MVC企业实战》(二) MVC开发前奏
2019独角兽企业重金招聘Python工程师标准>>> 在上一篇"<ASP.NET MVC企业级实战>(一)MVC开发前奏"中记录了作者介绍的一些 ...
- VS2010 C# 4.0新特性一览
终于静下心来仔细听了一遍Anders Hejlsberg(Visual Studio组的TECHNICAL FELLOW,C#的设计者之一)在PDC08上讲的"The Future of C ...
最新文章
- 获得PMP证书的这一年
- const深度总结(effective C++)
- sock 文件方式控制宿主机_sock
- 高级数据结构实现——自顶向下伸展树
- activiti脚本任务_Activiti中的安全脚本如何工作
- SQL Server 2017 AlwaysOn AG 自动初始化(十二)
- javascript加密七种方法
- python修改zip文件内容_windows-将zip文件内容提取到Python 2.7中的特定目录
- 【Spring第五篇】Autowired:自动装配
- 手把手教你0基础C语言速通
- google 常用的技术搜索关键词
- 角色权限管理系统(角色功能授权)
- django LookUp
- 美团token解决思路
- freemarker基于docx格式创建模板导出带图片pdf文件
- 高数_证明_罗尔定理
- Scrape Center爬虫平台之ssr3案例
- 【报告分享】2021年5G应用场景研究-CTR(附下载)
- XLSX工作表日期转文本变为数字问题解决
- 3D建模教程讲解!3ds Max电吉他制作