简体   繁体   English

为确保GetHashCode()方法返回对象的唯一值,应遵循哪些规则?

[英]What are the rules I should follow to ensure GetHashCode() method returns unique value for an object?

What are the rules I should follow to ensure GetHashCode() method returns unique value for an object? 为确保GetHashCode()方法返回对象的唯一值,应遵循哪些规则?

For example: 例如:

  • Should I include some prive members for the calculation? 我应该包括一些私有成员进行计算吗?
  • Should I multiply instead of sum? 我应该乘而不是总和吗?
  • Can I be sure that I am generating a uniqe hash code for a particular object graph? 我可以确定我正在为特定对象图生成唯一哈希码吗? etc. 等等

You shouldn't even aim for GetHashCode() returning a unique value for each object. 你甚至不应该瞄准 GetHashCode()返回的每个对象的唯一值。 That's not the point of GetHashCode() . 这不是 GetHashCode()

Eric Lippert has a great post about hash codes which you should read thoroughly. 埃里克·利珀特(Eric Lippert)撰写了一篇很棒的有关哈希码的文章 ,您应该仔细阅读。 Basically you want to end up with something which will always return the same value for two equal objects (and you need to work out what you mean by equal) and is likely to return different values for two non-equal objects. 基本上,您希望最终得到的东西将始终为两个相等的对象返回相同的值(并且您需要计算出相等的含义),并且可能为两个非相等的对象返回不同的值。

Personally I tend to use an implementation like this: 我个人倾向于使用这样的实现:

public override int GetHashCode()
{
    int hash = 17;
    hash = hash * 31 + field1.GetHashCode();
    hash = hash * 31 + field2.GetHashCode();
    hash = hash * 31 + field3.GetHashCode();
    ...
    return hash;
}

Things to watch out for: 注意事项:

  • If you have mutable objects, be careful! 如果您有易变的物品,请当心! You shouldn't mutate an object after using it as the key in a hash map. 在将对象用作哈希映射中的键之后,您不应该对其进行突变。
  • If your fields can be null, you need to check for that while calculating your hash. 如果您的字段可以为null,则需要在计算哈希值时检查该字段。 For example: 例如:

     hash = hash * 31 + (field2 == null ? 0 : field2.GetHashCode()); 

You don't necessarily need a fool proof hashcode because you also need to override Equals for comparison. 您不一定需要简单的哈希码,因为您还需要覆盖Equals进行比较。 Usually what I do is take the values I know are different across objects, concatenate them into a string and return the hash for that. 通常,我要做的是将我知道的值在对象之间不同,将它们连接成字符串并返回该哈希值。

I think your answer is here: See Jon Skeet answer, generally pretty reliable way to calculate it. 我认为您的答案在这里:请参阅Jon Skeet答案,这是一种相当可靠的计算方法。 Proved by time :) 时间证明:)

What is the best algorithm for an overridden System.Object.GetHashCode? 重写System.Object.GetHashCode的最佳算法是什么?

暂无
暂无

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

相关问题 Object.GetHashCode()对引用或值是唯一的吗? - Is Object.GetHashCode() unique to a reference or a value? GetHashCode为不同的对象返回相同的值。 是否有通过特定属性标识对象的方法? - GetHashCode returns the same value for different objects. Is there any method to identify object by particular properties? 确保GetHashCode()重载对于半相等的R3浮点向量返回相同的方法 - Method to ensure GetHashCode() overload returns the same for semi-equal R3 float vectors 当对象的标识符为空时,GetHashCode应该返回什么? - What should GetHashCode return when object's identifier is null? GetHashCode()方法应该注意作为参数给出的null值吗? - Should GetHashCode() method take care about null value given as parameter? class 应遵循哪些规则和约定才能与 NHibernate 正常工作? - What rules and conventions should a class follow to be able to work properly with NHibernate? 我为什么不*覆盖GetHashCode()? - Why should I *not* override GetHashCode()? 覆盖C#中的Object.Equals()实例方法;现在代码分析/ FxCop警告CA2218:“还应该重新定义GetHashCode”。我该抑制吗? - Overriding Object.Equals() instance method in C#; now Code Analysis/FxCop warning CA2218: “should also redefine GetHashCode”. Should I suppress this? 当方法返回无效结果时,我应该抛出什么异常 - What exception should I throw when method returns invalid result 当工厂方法返回null时,应该抛出什么异常? - What exception should I throw when a factory method returns null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM