Func <TObject,bool>还是Predicate <TObject>?

Mic*_*ith 8 c# linq lambda delegates

我一次又一次地看到API(特别是在.NET框架中)使用的Func<TObject, bool>时候Predicate<TObject>看似是一个完全负责任的选择.API设计师可能有什么好的理由呢?

Jon*_*eet 7

在LINQ中,Func<T, bool>用于Where使得索引以及元素的另一个重载是一致的:

IEnumerable<T> Where(IEnumerable<T> source, Func<T, bool> predicate)
IEnumerable<T> Where(IEnumerable<T> source, Func<T, int, bool> predicate)
Run Code Online (Sandbox Code Playgroud)

就个人而言,我认为该名称Predicate更具描述性,因此会在没有上述问题的一致性问题的情况下使用它.请注意,有一些事情要说,只需要了解ActionFunc委托类型......