当覆盖GetHash code()? [英] When to override GetHashCode()?

查看:111
本文介绍了当覆盖GetHash code()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应该在什么时候覆盖所提供的 GetHash code ()方法对象类的系统的命名空间?

When should we override the GetHashCode() method provided by 'Object' class in 'System' namespace?

推荐答案

在覆盖等于,基本上是这样。当你要提供平等的不是简单的参考平等的不同的想法。

When you override Equals, basically. When you want to provide a different idea of equality than simple reference equality.

字符串就是一个很好的例子 - 两个字符串是相等的(在一个简单的等号电话),如果他们再present相同的字符序列。散列code反映了这一点,所以如果两个字符串相等,他们将有相同的哈希code。 (反向未必是真实的 - 两个不相等的字符串的可以的具有相同的哈希code,但它不太可能)

String is a good example of this - two strings are equal (under a simple Equals call) if they represent the same sequence of characters. The hash code reflects this, such that if two strings are equal they will have the same hash code. (The reverse isn't necessarily true - two unequal strings can have the same hash code, but it's unlikely.)

(字符串是棘手的在其他方面,你要知道 - 有很多的平等基础上的文化和外壳,不同的想法,但 String.Equals 只是看起来在UTF -16 code点,从而弥补了该字符串,并将其在最简单的可能的方式进行比较。)

(Strings are tricky in other ways, mind you - there are lots of different ideas of equality based on culture and casing, but String.Equals just looks at the UTF-16 code points which make up the string, and compares them in the simplest conceivable fashion.)

这篇关于当覆盖GetHash code()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆