简体   繁体   English

如果不需要 GetHashCode() 虚拟实现

[英]GetHashCode() dummy implementation if not needed

I have already read tons of articles on properly implementing Equals() and GetHashCode() .我已经阅读了大量关于正确实现Equals()GetHashCode() I have some classes with lots of member variables for which I had to implement an Equals() method.我有一些类有很多成员变量,我必须为它们实现Equals()方法。 So far, so good.到现在为止还挺好。 However, when coming to implementing GetHashCode() I always get stuck finding a good implementation.然而,在实现GetHashCode()我总是无法找到一个好的实现。 As I know for certain classes that they will never be used in a dictionary or hash table, I didn't want to invest time in writing a good GetHashCode() implementation so I came up with the following dummy "default" implementation:正如我所知,对于某些类,它们永远不会在字典或哈希表中使用,我不想花时间编写一个好的GetHashCode()实现,所以我想出了以下虚拟“默认”实现:

public override int GetHashCode()
{
    Debug.Fail( "If you see this message, implement GetHashCode() properly" );

    // Always return the same value for all instances of this class. This ensures
    // that two instances which are equal (in terms of their Equals() method)
    // have the same hash code.
    return 0;
}

I wanted to ask for your opinions if this is a feasible approach.我想征求您的意见,如果这是一种可行的方法。

On one hand, I don't want to spend time on implementing GetHashCode() when I don't have to.一方面,我不想花时间在不需要的时候实现GetHashCode() On the other hand, I want my code to be clean and valid.另一方面,我希望我的代码干净有效。 The above implementation ensures that two instances which have the same Equals() result also have the same hash code.上述实现确保具有相同Equals()结果的两个实例也具有相同的哈希码。 Of course if it really would be used as a key in a dictionary, the performance would be bad.当然,如果真的要作为字典的key,性能会很差。 But for this case you would get the debug assertion.但是对于这种情况,您将获得调试断言。

Are there any drawbacks in the above approach which I might have missed?上述方法是否有任何我可能错过的缺点?

If you really don't want to implement it, I would recommend throwing an exception instead (eg NotImplementedException ) - then you're not just relying on Debug.Fail working.如果您真的不想实现它,我建议您改为NotImplementedException异常(例如NotImplementedException ) - 那么您不仅仅依赖于Debug.Fail工作。 If you end up in this situation in a production environment, then failing quickly may well be the best option.如果您最终在生产环境中遇到这种情况,那么快速失败可能是最好的选择。

But to be honest, once you've implemented equality it's usually really easy to write GetHashCode ( with code like this, perhaps ) - and if it's not easy then that's often a sign that your Equals method will violate its contract anyway.但老实说,一旦你实现了相等,通常很容易编写GetHashCode可能是这样的代码) - 如果这容易,那么这通常表明你的Equals方法无论如何都会违反其契约。

Note that GetHashCode isn't just used for dictionaries - if you try to join two lists in LINQ based on a key of your type, that will use GetHashCode too.请注意, GetHashCode不仅仅用于字典 - 如果您尝试根据您的类型键在 LINQ 中连接两个列表,则将使用GetHashCode Basically, not implementing it violates the principle of least astonishment.基本上,不执行它违反了最小惊讶原则。 Unless you're the only person using your code (and you have a perfect memory or are willing to check each time you use the type), you should implement it properly, IMO.除非您是唯一使用您的代码的人(并且您有完美的记忆力或愿意在每次使用该类型时进行检查),否则您应该正确实现它,IMO。

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

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