简体   繁体   English

c#是否与Java with equals和gethashcode()有相同的问题?

[英]Does c# have the same issues like Java with equals and gethashcode()?

Does c# have the same issues like Java with equals and gethashcode? c#是否与Java with equals和gethashcode有相同的问题?

issues like: http://onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html 问题如: http//onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html

If you mean that equality is hard to define when it comes to inheritance, yes. 如果你的意思是在继承方面很难定义平等,是的。

However, .NET has the IEqualityComparer<T> interface which is frequently used for equality and hashing - for containers such as Dictionary<TKey, TValue> . 但是,.NET具有IEqualityComparer<T>接口,该接口经常用于相等和散列 - 用于诸如Dictionary<TKey, TValue>容器。 Almost everything in the framework which needs equality/hashing allows the behaviour to be specified by an IEqualityComparer<T> , which means you can express whichever notion of equality you want, if the form given by the type itself is inappropriate for your needs. 几乎框架中需要相等/散列的所有内容都允许行为由IEqualityComparer<T>指定,这意味着如果类型本身给出的形式不适合您的需要,您可以表达您想要的任何相等概念。

Not sure which issues you're talking about since you're a little vague... 不确定你说的是哪些问题,因为你有点模糊......

But if you mean that you have should (if you don't want a compiler warning, or possibly unexpected behavior in your code) to override GetHashCode when you override Equals , then yes. 但是,如果你的意思是你应该 (如果你不想编译器警告,或者在你的代码可能是意外的行为)来覆盖GetHashCode ,当你重写Equals ,然后是。

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

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