本文介绍了为什么GetHash code是在Object类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 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.

在我的完美的世界:

平等(和哈希)在一般的继承层次原因的问题 - 只要你总是可以指定一种比较要使用(通过的IEqualityComparer< T> )和对象可以实现 IEquatable< T> 自己,如果他们想,我不明白为什么这应该是对象 EqualityComparer< T> .DEFAULT 可以使用的参考实现,如果 T 未实施 IEquatable&LT ; T> ,并推迟到对象除外。生活是令人愉快的。

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.

这篇关于为什么GetHash code是在Object类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:21