Dictionary.ContainsKey行为不端c#

Stu*_*ens 6 .net c# dictionary

我上了课 Column

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; }
}
Run Code Online (Sandbox Code Playgroud)

而且我在另一个班级中有以下字段

Dictionary<Column, string> dictionary
Run Code Online (Sandbox Code Playgroud)

在执行过程中,我得到了一个奇怪的行为dictionary.当我输入

dictionary.ContainsKey(dictionary.Keys.ToList()[1])
Run Code Online (Sandbox Code Playgroud)

我有false.
地球上怎么样?

UPDATED
我的Column类有GetHashCode或Equals Functions.以下是它们的实现:

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);
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 6

这不是一般问题,而是您的代码特有的问题.

更新:
字典键的哈希码不得更改.您的Column类不是这种情况,因为只要其任何属性发生更改,哈希代码也会更改.
文件:

只要对象在Dictionary中用作键,就不能以任何影响其哈希值的方式进行更改.

其背景如下:字典在添加密钥时检索并存储密钥的哈希码.如果密钥的哈希码在此之后发生了变化,则它与存储的哈希码不同,并且该对象将不会被视为等于最初插入的密钥.