views:

175

answers:

2

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

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

+3  A: 

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.

Justin Niessner
+11  A: 

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.

Jon Skeet
Issues like: http://onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html
Blankman
@Blankman: Right - so basically you want two different concepts of equality; one by ID and one by various fields. That's exactly what `IEqualityComparer<T>` is for.
Jon Skeet
so Collections use IEqualityComparer<T>? if yes, it seems a little more robust than Java.
Blankman
@Blankman: Yes, things like `HashSet<T>` allow you to specify a custom comparator when you create them.
Jon Skeet