Dictionary.ContainsKey misbehave C #

I have a Column class

 public class Column { public int Id { get; private set; } public string Name { get; private set; } public EPersonalCharacteristicType Type { get; private set; } public Column MainColumn { get; private set; } } 

and I have the following field in another class

 Dictionary<Column, string> dictionary 

In the middle of execution, I get the strange behavior of this dictionary . When i typed

 dictionary.ContainsKey(dictionary.Keys.ToList()[1]) 

I got false .
How can it be on earth?

UPDATED
My Column class has GetHashCode or Equals functions. Here are their implementations:

 public bool Equals(Column other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return other.Id == Id && Equals(other.Name, Name) && Equals(other.Type, Type) && Equals(other.MainColumn, MainColumn) && Equals(other.childColumns, childColumns); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != typeof (Column)) return false; return Equals((Column) obj); } public override int GetHashCode() { unchecked { int result = Id; result = (result*397) ^ (Name != null ? Name.GetHashCode() : 0); result = (result*397) ^ Type.GetHashCode(); result = (result*397) ^ (MainColumn != null ? MainColumn.GetHashCode() : 0); result = (result*397) ^ (childColumns != null ? childColumns.GetHashCode() : 0); return result; } } public static bool operator ==(Column left, Column right) { return Equals(left, right); } public static bool operator !=(Column left, Column right) { return !Equals(left, right); } 
+6
source share
2 answers

This is not a general problem, but something specific to your code.

UPDATE:
The hash of the dictionary key should not change. This does not apply to your Column class, because as soon as any of its properties is changed, the hash code will also change.
Documentation :

As long as the object is used as a key in the dictionary, it should not in any way change its hash value.

You can specify the following as a background: The dictionary retrieves and stores the key hash code at the time of its addition. If after that the key hash code has changed, it is different from the stored hash code and the object will not be considered equal to the original key inserted.

+6
source

If Dictionary<Column, string> right, then I would have guessed it because the Column type is incompatible with the way the Dictionary<TKey, TValue> checks equality - which is based on a comparison via GetHashCode and Equals - and my guess is that the Column type does not implement them.

Change the key type of dictionary to something more suitable for comparisons and equality. Ideally use a string derived from the column name.

Update

Based on your code update, I assume that something about this column has changed since it entered the dictionary, due to which its β€œlive” hash code changed from what it was when it was first added to the dictionary, there can be any change in Id , Name , Type , MainColumn or ChildColumns , which leads to a change in their HashCodes.

+2
source

Source: https://habr.com/ru/post/891093/


All Articles