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); }
source share