开发者

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

开发者 https://www.devze.com 2022-12-26 23:11 出处:网络
Does c# have the same issues like Java with equals and gethashcode? issues like: http://onjava.com/pub/a/onjava/20开发者_JS百科06/09/13/dont-let-hibernate-steal-your-identity.htmlIf you mean that equ

Does c# have the same issues like Java with equals and gethashcode?

issues like: http://onjava.com/pub/a/onjava/20开发者_JS百科06/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>. 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号