简体   繁体   English

C#.NET GetHashCode函数问题

[英]C# .NET GetHashCode function question

Hi I have a class with 6 string properties. 嗨我有一个有6个字符串属性的类。 A unique object will have different values for atleast one of these fields 对于这些字段中的至少一个,唯一对象将具有不同的值

To implement IEqualityComparer's GetHashCode function, I am concatenating all 6 properties and calling the GetHashCode on the resultant string. 为了实现IEqualityComparer的GetHashCode函数,我连接所有6个属性并在结果字符串上调用GetHashCode。

I had the following doubts: 我有以下疑惑:

  1. Is it necessary to call the GetHashcode on a unique value? 是否有必要在唯一值上调用GetHashcode?
  2. Will the concatenation operation on the six properties make the comparison slow? 对六个属性的连接操作是否会使比较缓慢?
  3. Should I use some other approach? 我应该使用其他方法吗?

If your string fields are named af and known not to be null, this is ReSharper's proposal for your GetHashCode() 如果您的字符串字段名为af并且已知为null,则这是ReSharper对GetHashCode()的建议

public override int GetHashCode() {
  unchecked {
    int result=a.GetHashCode();
    result=(result*397)^b.GetHashCode();
    result=(result*397)^c.GetHashCode();
    result=(result*397)^d.GetHashCode();
    result=(result*397)^e.GetHashCode();
    result=(result*397)^f.GetHashCode();
    return result;
  }
}

GetHashCode() should return the same hash code for all objects that return true if you call Equals() on those objects. 如果在这些对象上调用Equals(),则GetHashCode()应为返回true的所有对象返回相同的哈希码。 This means, for example, that you can return zero as the hash code regardless of what the field values are. 这意味着,例如,无论字段值是什么,您都可以返回零作为哈希码。 But that would make your object very inefficient when stored in data structures such as hash tables. 但是当存储在诸如散列表之类的数据结构中时,这将使您的对象效率非常低。

Combining the strings is one option, but note that you could for example combine just two of the stringsfor the hash code (while still comparing all the strings in equals!). 组合字符串是一个选项,但请注意,例如,您可以只为哈希代码组合两个字符串(同时仍然比较equals中的所有字符串!)。

You can also combine the hashes of the six separate strings, rather than computing a single hash for a combined string. 您还可以组合六个单独字符串的哈希值,而不是为组合字符串计算单个哈希值。 See for example Quick and Simple Hash Code Combinations 请参阅例如快速和简单哈希代码组合

I'm not sure if this will be significantly faster than concatenating the string. 我不确定这是否会比连接字符串快得多。

GetHashCode does not need to return unequal values for "unequal" objects. GetHashCode不需要为“不等”对象返回不相等的值。 It only needs to return equal values for equal objects (it also must return the same value for the lifetime of the object). 它只需要为相等的对象返回相等的值(它也必须在对象的生命周期内返回相同的值)。

This means that: 这意味着:

  1. If two objects compare as equal with Equals , then their GetHashCode must return the same value. 如果两个对象与Equals相等,那么它们的GetHashCode必须返回相同的值。
  2. If some of the 6 string properties are not strictly read-only, they cannot take part in the GetHashCode implementation. 如果6个字符串属性中的某些属性不是严格只读的,则它们不能参与GetHashCode实现。

If you cannot satisfy both points at the same time, you should re-evaluate your design because anything else will leave the door open for bugs. 如果你不能同时满足这两个要点,你应该重新评估你的设计,因为其他任何东西都会让你的门被打开。

Finally, you could probably make GetHashCode faster by calling GetHashCode on each of the 6 strings and then integrating all 6 results in one value using some bitwise operations. 最后,您可以通过在6个字符串中的每个字符串上调用GetHashCode然后使用一些按位操作将所有6个结果集成到一个值中来使GetHashCode更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM