Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func<TObject, bool> or Predicate<TObject>?

I've seen time and time again API (particularly in the .NET framework) that uses Func<TObject, bool> when Predicate<TObject> is seemingly a perfectly responsible option. What good reasons might an API designer have for doing so?

like image 737
Michael Smith Avatar asked Apr 18 '09 09:04

Michael Smith


1 Answers

In LINQ, Func<T, bool> is used for things like Where so that the other overload which takes the index as well as the element is consistent:

IEnumerable<T> Where(IEnumerable<T> source, Func<T, bool> predicate)
IEnumerable<T> Where(IEnumerable<T> source, Func<T, int, bool> predicate)

Personally I think the name Predicate is more descriptive, so I would use it in situations where there's no consistency issue like the one above. Mind you, there's something to be said for only needing to know about the Action and Func delegate types...

like image 198
Jon Skeet Avatar answered Oct 23 '22 11:10

Jon Skeet