为什么 GetHashCode 不是像 .NET 中的 HashCode 那样的属性

发布于 2024-07-13 03:07:11 字数 50 浏览 7 评论 0原文

为什么 GetHashCode 不是像 .NET 中的 HashCode 那样的属性?

Why GetHashCode is not a property like HashCode in .NET?

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

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

发布评论

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

评论(6

浸婚纱 2024-07-20 03:07:11

可能是因为它需要计算,并将其公开为属性可能意味着哈希码已经可以免费使用。

编辑:
相关指南:属性与方法

"该操作的成本足够高,您希望告知用户他们应该考虑缓存结果。”

也许在某些情况下 GetHashCode 已经足够昂贵了。

Probably because it requires computation, and exposing it as a propery might imply that the hashcode is already available for free.

Edit:
Guidelines on this: Properties versus Methods

"The operation is expensive enough that you want to communicate to the user that they should consider caching the result."

Perhaps GetHashCode is expensive enough in some cases.

吲‖鸣 2024-07-20 03:07:11

我认为没有什么充分的理由。 GetHashCode 的任何实现都应该足够快以放入属性中。 也就是说,.Net 框架中存在大量设计缺陷,有些很小,有些很严重。 这似乎是一件小事。

I don't think there's any good reason. Any implemention of GetHashCode should be fast enought to put into a property. That said, there are plenty of design flaws in the .Net framework, some small, some serious. This seems like a small one.

夜未央樱花落 2024-07-20 03:07:11

通常不可能为这样的类定义 HashCode:

例如,类的对象不
有一个明确的概念
身份。

因此,让 GetHashCode() 方法抛出 NotImplementedException 是很常见的。 如果 HashCode 是一个属性,这当然会出现各种各样的问题,因为大多数人(和调试器)认为获取属性的值总是有效的

Often it is not possible to define a HashCode for a class that makes since:

e.g. the objects of the class don’t
have a well defined concept of
identity.

Therefore it is common to make the GetHashCode() method throw a NotImplementedException. This would course all sort of problem if HashCode was a property, as most people (and debuggers) assume it is always valid to get the value of a property

再可℃爱ぅ一点好了 2024-07-20 03:07:11

除此之外,属性只不过是 getter 和 setter 方法,从设计角度来看,属性不应该包含初始化或验证之外的任何计算,例如:

private object _obj;
public object Obj
{
  get
  {
    if(_obj == null)
    {
      _obj = new object();
    }
    return _obj;
  }
  set
  {
    if(value == badvalue)
    {
      throw new ArgumentException("value");
    }
    _obj = value;
  }
}

GetHashCode() 不包含大量计算,但它可以包含如此长时间的运行操作(仅仅因为它可以以复杂的方式计算对象的哈希码),这就是为什么它是方法而不是属性。

Besides that a property is nothing else than a getter and a setter method, from a design perspective a property should never contain any computations other than initializing or validation, eg:

private object _obj;
public object Obj
{
  get
  {
    if(_obj == null)
    {
      _obj = new object();
    }
    return _obj;
  }
  set
  {
    if(value == badvalue)
    {
      throw new ArgumentException("value");
    }
    _obj = value;
  }
}

GetHashCode() does not contain extensive computations, but it could contain such long running operations (just from the fact that it could compute the hashcode of an object in a complex manner), this is why its a method instead of a property.

小ぇ时光︴ 2024-07-20 03:07:11

仅当属性背后的计算非常快或已缓存时才应使用属性

,此外,属性中的唯一逻辑应该是验证

properties should only be used if the computation behind them is really fast or cached

besides most of the time the only logic in properties should be validation

孤蝉 2024-07-20 03:07:11

您必须记住,.NET Framework 被设计为可供多种语言访问。

理论上,您可以创建一个无法正确覆盖属性的编译器。 虽然这会导致编译器相当蹩脚,但它不一定是非法的。 (记住属性只是带有一些元数据的方法)

You have to remember that the .NET Framework is designed to be accessed by a wide variety of languages.

In theory you could create a compiler that is incapable of correctly overriding properties. While that would make for a pretty crappy compiler, it would not necessarily be illegal. (Remember properties are just methods with some meta data)

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