还没有发射了反射镜来看待差异,但就一个期望看到的完全相同的编码进行比较时 Func<T, bool>Predicate<T>

我猜想没有差别作为这两个采取一个一般参数和返回bool?

有帮助吗?

解决方案

它们共享同样的签名,但他们仍然不同的类型。

其他提示

Robert S.是完全正确的;例如:-

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");
  }
}

更多的灵活 Func 家庭只到达了。净3.5,所以它将在功能上的重复类型,必须包括早期的必要性。

(再加上的名字 Predicate 通信的使用的读者的源代码)

即使没有仿制药,可以有不同的委托类型相同的签署和返回的类型。例如:

namespace N
{
  // Represents a method that takes in a string and checks to see
  // if this string has some predicate (i.e. meets some criteria)
  // or not.
  internal delegate bool StringPredicate(string stringToTest);

  // Represents a method that takes in a string representing a
  // yes/no or true/false value and returns the boolean value which
  // corresponds to this string
  internal delegate bool BooleanParser(string stringToConvert);
}

在上述例子中,两个非通用的类型具有相同的签名和返回的类型。(实际上也一样 Predicate<string>Func<string, bool>).但正如我试图表明,"意义"的两个是不同的。

这有点像如果我做两个班, class Car { string Color; decimal Price; }class Person { string FullName; decimal BodyMassIndex; }, 然后只是因为他们两个人住了 string 和一个 decimal, 那并不意味着它们是"相同"的类型。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top