不是 Func和谓词编译后同样的事情吗?

发布于 2024-07-05 04:31:05 字数 147 浏览 13 评论 0原文

尚未启动反射器来查看差异,但在比较 FuncPredicate 时,人们希望看到完全相同的编译代码

我想没有什么区别,因为两者都采用通用参数并返回 bool?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一片旧的回忆 2024-07-12 04:31:05

更灵活的 Func 系列仅在 .NET 3.5 中出现,因此它将在功能上复制之前出于必要而必须包含的类型。

(加上名称 Predicate 向源代码的读者传达预期用途)

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)

小情绪 2024-07-12 04:31:05

即使没有泛型,您也可以拥有签名和返回类型相同的不同委托类型。 例如:

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

在上面的示例中,两个非泛型类型具有相同的签名和返回类型。 (实际上也与 PredicateFunc 相同)。 但正如我试图指出的,两者的“含义”是不同的。

这有点像我创建两个类,class Car { string Color; 小数价格; }class Person { string FullName; 十进制身体质量指数; },那么仅仅因为它们都包含一个字符串和一个小数,并不意味着它们是“相同”类型。

Even without generics, you can have different delegate types that are identical in signatures and return types. For example:

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

In the above example, the two non-generic types have the same signature and return type. (And actually also the same as Predicate<string> and Func<string, bool>). But as I tried to indicate, the "meaning" of the two are different.

This is somewhat like if I make two classes, class Car { string Color; decimal Price; } and class Person { string FullName; decimal BodyMassIndex; }, then just because both of them hold a string and a decimal, that doesn't mean they're the "same" type.

梦过后 2024-07-12 04:31:05

它们具有相同的签名,但仍然是不同的类型。

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

蓬勃野心 2024-07-12 04:31:05

罗伯特·S. (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");
  }
}

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");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文