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

时间:2021-04-01 16:50:18

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

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

6 个解决方案

#1


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.

我认为没有任何理由。 GetHashCode的任何实现都应该快速投入到属性中。也就是说,.Net框架中有很多设计缺陷,有些小,有些严重。这看起来很小。

#2


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.

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

#3


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

通常,无法为以下类创建HashCode:

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

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

#4


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:

除此之外,属性只不过是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() 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.

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

#5


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

除了大多数时候,属性中唯一的逻辑应该是验证

#6


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

您必须记住,.NET Framework旨在通过各种语言进行访问。

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)

从理论上讲,您可以创建一个无法正确覆盖属性的编译器。虽然这会使一个非常糟糕的编译器,但它不一定是非法的。 (记住属性只是带有一些元数据的方法)

#1


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.

我认为没有任何理由。 GetHashCode的任何实现都应该快速投入到属性中。也就是说,.Net框架中有很多设计缺陷,有些小,有些严重。这看起来很小。

#2


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.

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

#3


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

通常,无法为以下类创建HashCode:

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

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

#4


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:

除此之外,属性只不过是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() 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.

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

#5


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

除了大多数时候,属性中唯一的逻辑应该是验证

#6


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

您必须记住,.NET Framework旨在通过各种语言进行访问。

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)

从理论上讲,您可以创建一个无法正确覆盖属性的编译器。虽然这会使一个非常糟糕的编译器,但它不一定是非法的。 (记住属性只是带有一些元数据的方法)