一般的建议,并就如何正确地覆盖object.GetHash code指引() [英] General advice and guidelines on how to properly override object.GetHashCode()

查看:126
本文介绍了一般的建议,并就如何正确地覆盖object.GetHash code指引()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN ,哈希函数必须有以下性质:

  
      
  1. 如果两个对象的比较结果相等,每个对象的GetHash code方法必须返回相同的值。但是,如果两个对象不比较结果相等,这两个对象GetHash code方法不必返回不同的值。

  2.   
  3. 对于一个对象必须一致地返回相同的散列code,只要没有修改对象的状态,确定对象的equals方法的返回值的GetHash code方法。注意,这是真实的,只有一个应用程序的当前执行的,并且不同的哈希code能如果应用程序再次运行被返回

  4.   
  5. 为了获得最佳性能,哈希函数必须生成一个随机分布于所有的输入。

  6.   


我一直在寻找自己在以下情形:我创建了一个类,实施 IEquatable< T> 和覆盖的Object.Equals(对象) MSDN 说:

  

这是重写equals还必须重写GetHash code型;否则,哈希表可能无法正常工作。

然后它通常会停止了一下我。因为,你如何正确地覆盖 object.GetHash code()?永远不知道从哪里开始,这似乎是一个很大的陷阱。

下面的计算器,还有与GetHash code重写了不少问题,但大多似乎是相当特殊的情况和具体问题。所以,所以我想在这里得到很好的汇编。与一般的咨询和指​​导的概述。怎么做,什么不该做,常见的陷阱,从哪里开始,等等。

我想它在C#来特别是定向的,但我想它会工作一种以相同的方式为其他.NET语言以及(?)。


我想,也许是最好的办法是先创建每个主题一个答案一个快速和简单的答案(接近一行程序,如果可能的话),那么也许一些更多的信息,并与相关的提问,讨论, ,博客文章等,如果有的话。然后,我可以创造一个职位作为接受的答案(得到它在上面),只需表的内容。尽量保持它简明扼要。不要只是链接到其他问题和博客文章。尝试采取其中的精髓,然后而是链接到源(尤其是因为源可能会消失。另外,请尝试编辑非常相似的人创造了大量的完善,而不是答案。

我不是一个很好的技术作家,但我至少会尝试格式的答案,使他们看起来很相像,创建表的内容,等等。我也将尝试一些相关问题搜索在这里在使回答的这些部分,也​​许拉出的那些我可以管理的精髓。但是,因为我不是很稳定,对这个话题,我会尽量远离大部分:P

解决方案

目录

  • <一个href="http://stackoverflow.com/questions/1378686/general-advice-and-guidelines-on-how-to-properly-override-object-gethash$c$c/1378725#1378725">When我重写 object.GetHash code

  • <一个href="http://stackoverflow.com/questions/1378686/general-advice-and-guidelines-on-how-to-properly-override-object-gethash$c$c/1378796#1378796">Why我必须重写object.GetHash code()?

  • <一个href="http://stackoverflow.com/questions/1378686/general-advice-and-guidelines-on-how-to-properly-override-object-gethash$c$c/1378895#1378895">What有见于GetHash code实现这些神奇数字?


的事情,我想被覆盖,但一直尚未:

  • 如何创建整数(如何转换的对象变成一个int不是很明显,我反正)。
  • 在哪些领域立足于散列code。
    • 如果它应该只对不可变的领域,如果只有可变的?
  • 如何产生了良好的随机分布。 (MSDN物业#3)
    • 部分到此,似乎是要选择好神奇的素数(看到17,23和397被使用过),但你如何选择呢,什么是它是什么呢?
  • 如何确保散列code保持不变全部​​通过对象的生命周期。 (MSDN物业#2)
    • 特别是当平等是基于可变领域。 (MSDN物业#1)
  • 如何处理那些复杂类型的字段(并不在内置在C#类型 )。
    • 在复杂的对象和结构,数组,集合,列表,字典,泛型类型,等等。
    • 例如,即使列表或字典可能是只读的,这并不意味着内容的它是。
  • 如何处理继承的类。
    • 如果你不小心将 base.GetHash code()到您的散列code?
  • 你能不能在技术上只是懒惰,返回0?会严重打破MSDN方针是#3,但至少会确保#1和#2总是正确的:P
  • 常见的问题和陷阱。

According to MSDN, a hash function must have the following properties:

  1. If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

  2. The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.

  3. For the best performance, a hash function must generate a random distribution for all input.


I keep finding myself in the following scenario: I have created a class, implemented IEquatable<T> and overridden object.Equals(object). MSDN states that:

Types that override Equals must also override GetHashCode ; otherwise, Hashtable might not work correctly.

And then it usually stops up a bit for me. Because, how do you properly override object.GetHashCode()? Never really know where to start, and it seems to be a lot of pitfalls.

Here at StackOverflow, there are quite a few questions related to GetHashCode overriding, but most of them seems to be on quite particular cases and specific issues. So, therefore I would like to get a good compilation here. An overview with general advice and guidelines. What to do, what not to do, common pitfalls, where to start, etc.

I would like it to be especially directed at C#, but I would think it will work kind of the same way for other .NET languages as well(?).


I think maybe the best way is to create one answer per topic with a quick and short answer first (close to one-liner if at all possible), then maybe some more information and end with related questions, discussions, blog posts, etc., if there are any. I can then create one post as the accepted answer (to get it on top) with just a "table of contents". Try to keep it short and concise. And don't just link to other questions and blog posts. Try to take the essence of them and then rather link to source (especially since the source could disappear. Also, please try to edit and improve answers instead of created lots of very similar ones.

I am not a very good technical writer, but I will at least try to format answers so they look alike, create the table of contents, etc. I will also try to search up some of the related questions here at SO that answers parts of these and maybe pull out the essence of the ones I can manage. But since I am not very stable on this topic, I will try to stay away for the most part :p

解决方案

Table of contents


Things that I would like to be covered, but haven't been yet:

  • How to create the integer (How to "convert" an object into an int wasn't very obvious to me anyways).
  • What fields to base the hash code upon.
    • If it should only be on immutable fields, what if there are only mutable ones?
  • How to generate a good random distribution. (MSDN Property #3)
    • Part to this, seems to choose a good magic prime number (have seen 17, 23 and 397 been used), but how do you choose it, and what is it for exactly?
  • How to make sure the hash code stays the same all through the object lifetime. (MSDN Property #2)
    • Especially when the equality is based upon mutable fields. (MSDN Property #1)
  • How to deal with fields that are complex types (not among the built-in C# types).
    • Complex objects and structs, arrays, collections, lists, dictionaries, generic types, etc.
    • For example, even though the list or dictionary might be readonly, that doesn't mean the contents of it are.
  • How to deal with inherited classes.
    • Should you somehow incorporate base.GetHashCode() into your hash code?
  • Could you technically just be lazy and return 0? Would heavily break MSDN guideline number #3, but would at least make sure #1 and #2 were always true :P
  • Common pitfalls and gotchas.

这篇关于一般的建议,并就如何正确地覆盖object.GetHash code指引()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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