Advantage of Predicate <T> over Func <T, bool>?
Is there any value for using a predicate over a normal delegate? Taking the example below, I do not see.
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");
+5
5 answers
They are almost the same. Predicate<T>is the type of delegate that has been added to the base class library for list methods and the Find () array. This was before LINQ when a more general family of delegates was introduced Func. You can even write your own delegate type and use it:
delegate bool IntPredicate(int x);
static void Main()
{
IntPredicate isEven = delegate(int x) {return x % 2 == 0;};
Console.WriteLine(isEven(1) + "\r\r");
}
+5