为什么 GetHash code 是Object类的一部分?的类中的对象的唯一小部分被用来作为哈希表中的键。那岂不是更好有必须实现一个单独的接口,当我们想要的类的对象作为哈希表键。
Why GetHashCode is part of the Object class? Only small part of the objects of the classes are used as keys in hash tables. Wouldn't it be better to have a separate interface which must be implemented when we want objects of the class to serve as keys in hash table.
必须有一个原因,MS团队决定将这一方法在Object类中,从而使其可无处不在。
There must be a reason that MS team decided to include this method in Object class and thus make it available "everywhere".
这是一个设计错误,从Java拷贝,海事组织。
It was a design mistake copied from Java, IMO.
在我的完美的世界:
的ToString
将改名 ToDebugString
来设置适当的期望
等于
和 GetHash code
将消失
将有一个 ReferenceEqualityComparer
实施的IEqualityComparer&LT的; T>
:将等于这部分是很容易的一会儿,但没有办法得到一个原始的散列code,如果它的覆盖的
在对象不会有与之关联的监视器:显示器
将有一个构造,而输入
/ 退出
等将是实例方法。
ToString
would be renamed ToDebugString
to set expectations appropriately
Equals
and GetHashCode
would be gone
There would be a ReferenceEqualityComparer
implementation of IEqualityComparer<T>
: the equals part of this is easy at the moment, but there's no way of getting an "original" hash code if it's overridden
Objects wouldn't have monitors associated with them: Monitor
would have a constructor, and Enter
/Exit
etc would be instance methods.
平等(和哈希)在一般的继承层次原因的问题 - 只要你总是可以指定一种比较要使用(通过的IEqualityComparer&LT; T&GT;
)和对象可以实现 IEquatable&LT; T&GT;
自己,如果他们想,我不明白为什么这应该是对象
。 EqualityComparer&LT; T&GT; .DEFAULT
可以使用的参考实现,如果 T
未实施 IEquatable&LT ; T&GT;
,并推迟到对象除外。生活是令人愉快的。
Equality (and thus hashing) cause problems in inheritance hierarchies in general - so long as you can always specify the kind of comparison you want to use (via IEqualityComparer<T>
) and objects can implement IEquatable<T>
themselves if they want to, I don't see why it should be on Object
. EqualityComparer<T>.Default
could use the reference implementation if T
didn't implement IEquatable<T>
and defer to the objects otherwise. Life would be pleasant.
不错啊。虽然我在这,数组协变是另一个平台上的错误。如果你想在C#语言的错误,我可以启动另一个次要的言论,如果你喜欢。)(这还是迄今为止我最喜欢的语言,但有件事情我想一直做不同的)
Ah well. While I'm at it, array covariance was another platform mistake. If you want language mistakes in C#, I can start another minor rant if you like ;) (It's still by far my favourite language, but there are things I wish had been done differently.)
我已经博客这个的其他地方,顺便说一句。
I've blogged about this elsewhere, btw.