C#中的Predicate<T>与Func<T, bool>
来源:互联网 发布:一级域名转二级域名 编辑:程序博客网 时间:2024/05/17 05:52
C#有很多语法糖,有时使用时会觉得模棱两可,
以前很少用Predicate,因为英文太长,不如Func实在,
在我感觉中,这两个本该就是同一个东西。
但你会发现:
(1)List类的所有方法如Exists, Find, FindAll, FindIndex等全是接受Predicate<T>类型参数的。
(2)而List类实现的接口IEnumerable的所有扩展方法如All,Any,First,FirstOrDefault,Where等都是接受Func<TSource, bool>类型的参数的。
同样是List中的方法,为什么会使用不同的委托类型呢?
先看一下定义:
Func<T, bool> :表示有传入T类型的参数,返回值为bool的委托
Predicate<T>:表示有传入T类型的参数,返回值为bool的委托
在定义上不存在任何差别。
查看Predicate和Func的定义如下:
public delegate bool Predicate<in T>(T obj);public delegate TResult Func<in T, out TResult>(T arg);当Func定义中TResult固定为bool,则两个定义应该是完全一致,
List<T>中的方法参数本应该统一才对。
是微软豆逼了么,一定要写成不一样的?谁能告诉我为什么!
下面验证一下:
static void Main(string[] args) { Predicate<int> myPredicate = i => i > 10; Func<int, bool> myFunc = i => i > 10; List<int> list = new List<int>(); list.Add(5); list.Add(9); list.Add(20); list.Add(30); List<int> newList = list.FindAll(myPredicate); List<int> newListFunc = list.Where(myFunc).ToList(); Console.ReadKey(); }
可以看到Predicate和Func接受的是完全相同的Lambada表达式,
而且执行结果newList和newListFunc完全相同。
此时用F12打开Where的定义就可以看到如下代码:
// // 摘要: // 基于谓词筛选值序列。 // // 参数: // source: // 要筛选的 System.Collections.Generic.IEnumerable`1。 // // predicate: // 用于测试每个元素是否满足条件的函数。 // // 类型参数: // TSource: // source 中的元素的类型。 // // 返回结果: // 一个 System.Collections.Generic.IEnumerable`1,包含输入序列中满足条件的元素。 // // 异常: // T:System.ArgumentNullException: // source 或 predicate 为 null。 public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
由扩展函数Where的参数定义【Func<TSource, bool> predicate】可见,
Func<TSource, bool> 即predicate,微软就是这么定义的。
结论:
Func<T, bool>是对delegate bool Predicate<T>(T obj)的简化,
Predicate<T>又是对Func<T, bool>的简化,
其实,就是一个东西。
List<T>中的方法应该统一参数。
附上以下内容:
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。这个是祖宗。
Func可以接受0个至16个传入参数,必须具有返回值。
Action可以接受0个至16个传入参数,无返回值。
Predicate只能接受一个传入参数,返回值为bool类型。
Func可以接受0个至16个传入参数,必须具有返回值。
Action可以接受0个至16个传入参数,无返回值。
Predicate只能接受一个传入参数,返回值为bool类型。
0 0
- C#中的Predicate<T>与Func<T, bool>
- Expression<Func<T, bool>>与Func<T, bool>的区别
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- Func<T, bool>
- C# Action<T>、Func<T>、Predicate<T>委托的区别、用法
- 【基础】C#:委托Action、Action<T>、Func<T>、Predicate<T>
- Func<T, bool> Func<T, object>小例子
- C# Func<T,TResult>
- C#中的Action<T>、Func<T>委托和Lambda表达式
- Expression<Func<T, bool>>拼接查询条件
- C#编程:Predicate<T> 委托
- C#中的Action<>和Func<>和Predicate
- Func<T,T> 委托 与 lambda 使用
- 拉姆达表达式 追加 条件判断 Expression<Func<T, bool>>
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
- C#委托之Action、Func与Predicate用法
- 对 set 的遍历
- new 外部类().成员
- 【NDN VANET】Rapid Traffic Information Dissemination Using Named Data 学习笔记
- c/c++整理--预处理
- 【C++学习笔记】指针和数组的基本等价测试
- C#中的Predicate<T>与Func<T, bool>
- Android 获取手机信息相关的方法
- log4cplus应用之日志文件输出(二)
- 学习笔记-Dilworth定理
- C++中vector中erase用法注意事项
- 02-线性结构4 Pop Sequence
- java 虚拟机基础介绍
- MATLAB求解导弹运动的一些基础方法
- 字符串的包含