gpt4 book ai didi

c# - 小类的好散列? (覆盖 GetHashCode)

转载 作者:行者123 更新时间:2023-11-30 12:53:00 29 4
gpt4 key购买 nike

我使用一些包含 1-2 个整数的身份类/结构,可能还有一个日期时间或一个小字符串。我将它们用作字典中的键。

对于这样的事情,什么是 GetHashCode 的良好覆盖?一些非常简单但仍然有一定性能的东西。

谢谢

最佳答案

查看 Essential C# .

它详细描述了如何正确覆盖GetHashCode()

本书摘录

The purpose of the hash code is to efficiently balance a hash table by generating a number that corresponds to the value of an object.

  • Required: Equal objects must have equal hash codes (if a.Equals(b), then a.GetHashCode() == b.GetHashCode())
  • Required: GetHashCode()'s returns over the life of a particular object should be constant (the same value), even if the object's data changes. In many cases, you should cache the method return to enforce this.
  • Required: GetHashCode() should not throw any exceptions; GetHashCode() must always successfully return a value.
  • Performance: Hash codes should be unique whenever possible. However, since hash code return only an int, there has to be an overlap in hash codes for objects that have potentially more values than an int can hold -- virtually all types. (An obvious example is long, since there are more possible long values than an int could uniquely identify.)
  • Performance: The possible hash code values should be distributed evenly over the range of an int. For example, creating a hash that doesn't consider the fact that distribution of a string in Latin-based languages primarily centers on the initial 128 ASCII characters would result in a very uneven distribution of string values and would not be a strong GetHashCode() algorithm.
  • Performance: GetHashCode() should be optimized for performance. GetHashCode() is generally used in Equals() implementations to short-circuit a full equals comparison if the hash codes are different. As a result, it is frequently called when the type is used as a key type in dictionary collections.
  • Performance: Small differences between two objects should result in large differences between hash codes values -- ideally, a 1-bit difference in the object results in around 16 bits of the hash code changing, on average. This helps ensure that the hash table remains balanced no matter how it is "bucketing" the hash values.
  • Security: It should be difficult for an attacker to craft an object that has a particular hash code. The attack is to flood a hash table with large amounts of data that all hash to the same value. The hash table implementation then becomes O(n) instead of O(1), resulting in a possible denial-of-service attack.

正如此处已经提到的,您还必须考虑有关重写 Equals() 的一些要点,并且有一些代码示例展示了如何实现这两个函数。

所以这些信息应该提供一个起点,但我建议购买这本书并阅读完整的第 9 章(至少前十二面)以获得关于如何正确实现这两个关键功能的所有要点。

关于c# - 小类的好散列? (覆盖 GetHashCode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3204155/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com