They might be different if the class SecurityUser
stores an ID that increases for each user you create. If the class uses this to calculate its HashCode, they will probably be different. You should not rely on GetHashCode
to test for equality between two objects.
The only requirement for GetHashCode
is that if objA.Equals(objB)
, then objA.GetHashCode() == objB.GetHashCode()
.
See this link (section "Notes to Implementers") for details on the implementation of GetHashCode()
, especially this paragraph:
- If two objects compare as equal, the GetHashCode method for each
object must return the same value. However, if two objects do not
compare as equal, the GetHashCode methods for the two object do not
have to return different values.
If GetHashCode()
is not overridden in SecurityUser
, the two HashCodes will be different as the two objects objA
and objB
are references to different objects in memory (as indicated by the new
-Keyword).