GetHashCode problem
Can anyone help explain the following. I have a problem with Dictionary where it ContainsKey
evaluates to false Equals
and GetHashCode
for objects it is successful. Below is the output from the direct window in Visual Studio:
?LocationToRackingGroup.Keys.ToArray()[23].Equals(location)
true
?LocationToRackingGroup.Keys.ToArray()[23] == (location)
true
?this.LocationToRackingGroup.ContainsKey(location)
false
Am I missing something? Any ideas are greatly appreciated.
source to share
Ok, I would like to look at a couple of things:
1: GetHashCode
correctly implemented:
?LocationToRackingGroup.Keys.ToArray()[23].GetHashCode() == location.GetHashCode()
2: if it's a generic vocabulary, does the type implement (explicitly) IEquatable<Location>
3: did you add the custom one IEqualityComparer<Location>
to the dictionary in the constructor?
To rule out the latter, perhaps look at:
?LocationToRackingGroup.Comparer.Equals(blah23, location); // should be true
?LocationToRackingGroup.Comparer.GetHashCode(blah23); // should equal below
?LocationToRackingGroup.Comparer.GetHashCode(location);// should equal above
source to share