(转)C#中的Predicate<T>与Func<T, bool>

  Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。这个是祖宗。
  Func可以接受0个至16个传入参数,必须具有返回值。
  Action可以接受0个至16个传入参数,无返回值。
  Predicate只能接受一个传入参数,返回值为bool类型。

public delegate bool Predicate<in T>(T obj);  
public delegate TResult Func<in T, out TResult>(T arg);  
Func<T, bool> :表示有传入T类型的参数,返回值为bool的委托
Predicate<T>:表示有传入T类型的参数,返回值为bool的委托
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完全相同。
 
http://blog.csdn.net/rye_grass/article/details/66041423  C#中的Predicate<T>与Func<T, bool>
posted @ 2018-01-07 11:42  BloggerSb  阅读(2360)  评论(0编辑  收藏  举报