I ran into a problem with GetHashCode and Equals, which I redefined for the class. I use the == operator to make sure they are both equal, and I would expect this to call both GetHashCode and Equals if their hash code is the same, to check if they are really equal.
But, to my surprise, neither the challenge nor the result of the equality test is false (although this should be true).
Override Code:
public class User : ActiveRecordBase<User> [...] public override int GetHashCode() { return Id; } public override bool Equals(object obj) { User user = (User)obj; if (user == null) { return false; } return user.Id == Id; } }
Equality Check:
if (x == y) // x and y are both of the same User class // I'd expect this test to call both GetHashCode and Equals
c # operator-overloading
tomzx
source share