Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't Func<T, bool> and Predicate<T> the same thing after compilation?

Tags:

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?

like image 603
Sean Chambers Avatar asked Aug 28 '08 16:08

Sean Chambers


2 Answers

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

like image 170
Robert S. Avatar answered Sep 19 '22 15:09

Robert S.


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");   } } 
like image 23
ljs Avatar answered Sep 21 '22 15:09

ljs