tags:

views:

702

answers:

3

Haven't fired up reflector to look at the difference but would one expect to see the exact same compiled code when comparing Func<T, bool> vs. Predicate<T>

I would imagine there is no difference as both take a generic parameter and return bool?

+11  A: 

They share the same signature, but they're still different types.

Robert S.
+8  A: 

Robert S. is completely correct; for example:-

class A {
  static void Main() {
    Func<int, bool> func = i => i > 100;
    Predicate<int> pred = i => i > 100;

    Test<int>(pred, 150);
    Test<int>(func, 150); // Error
  }

  static void Test<T>(Predicate<T> pred, T val) {
    Console.WriteLine(pred(val) ? "true" : "false");
  }
}
kronoz
Good way of showing the difference by example
RichardOD
A: 

The more flexible Func family only arrived in .NET 3.5, so it will functionally duplicate types that had to be included earlier out of necessity.

(Plus the name Predicate communicates the intended usage to readers of the source code)

frou