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类型。



0 0
原创粉丝点击