Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of Predicate<T> over Func<T,bool>?

Tags:

c#

.net

delegates

Is there any value to using a predicate over a normal delegate? Taking the example below, I don't see any.

Predicate<int> isEven = delegate(int x) { return x % 2 == 0; };
Console.WriteLine(isEven(1) + "\r\r");

Func<int, bool> isEven2 = delegate(int x) { return x % 2 == 0; };
Console.WriteLine(isEven(1) + "\r\r");
like image 868
Ryan Avatar asked Jan 04 '11 03:01

Ryan


1 Answers

They're effectively the same. Predicate<T> is a delegate type that was added to the base class library for list and array Find() methods. This was before LINQ, which was when the more general Func family of delegates were introduced. You could even write your own delegate type and use that:

delegate bool IntPredicate(int x); 

static void Main()
{
   IntPredicate isEven = delegate(int x) {return x % 2 == 0;};
   Console.WriteLine(isEven(1) + "\r\r");
}
like image 74
Mark Cidade Avatar answered Sep 23 '22 02:09

Mark Cidade