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");
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With