简体   繁体   English

Dictionary.ContainsKey行为不端c#

[英]Dictionary.ContainsKey misbehave c#

I've got a class Column 我有一个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; }
}

and I've got the following field in another class 而且我在另一个班级中有以下字段

Dictionary<Column, string> dictionary

In the middle of execution I get a strange behavior of this dictionary . 在执行过程中,我得到了这本dictionary的奇怪行为。 When I typed 当我输入

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

I got false . 我得到了false
How on Earth can it be? 地球上怎么样?

UPDATED 更新
my Column class has GetHashCode or Equals Functions. 我的Column类有GetHashCode或Equals Functions。 Here are the implementations of them: 以下是它们的实现:

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

This is not a general problem but something specific to your code. 这不是一般问题,而是您的代码特有的问题。

UPDATE: 更新:
The hash code of the key of a dictionary must not change. 字典键的哈希码不得更改。 This is not the case with your Column class, because as soon as any of its properties are changed, the hash code changes, too. 您的Column类不是这种情况,因为只要其任何属性发生更改,哈希代码也会更改。
Documentation : 文件

As long as an object is used as a key in the Dictionary, it must not change in any way that affects its hash value. 只要对象在Dictionary中用作键,就不能以任何影响其哈希值的方式进行更改。

The background of this is the following: The dictionary retrieves and stores the hash code of the key at the time it is added. 其背景如下:字典在添加密钥时检索并存储密钥的哈希码。 If the hash code of the key changed after that, it is different to the stored hash code and the object will not be seen as equal to the originally inserted key. 如果密钥的哈希码在此之后发生了变化,则它与存储的哈希码不同,并且该对象将不会被视为等于最初插入的密钥。

If Dictionary<Column, string> is right, then I would guess it's because the type Column is not compatible with how Dictionary<TKey, TValue> checks equality - which is based on comparisons through GetHashCode and Equals - and my guess is that the Column type does not implement these. 如果Dictionary<Column, string>是正确的,那么我猜它是因为类型ColumnDictionary<TKey, TValue>检查相等性的方式不兼容 - 这是基于通过GetHashCodeEquals比较 - 我的猜测是Column type没有实现这些。

Change the key type of the dictionary to something more suited to comparisons and equality. 将字典的键类型更改为更适合比较和相等的内容。 Ideally use a string derived from the column's name. 理想情况下使用从列名称派生的字符串。

Update 更新

Based on your code update, my guess is that something about the column has changed since it went into the dictionary that causes its 'live' hash code to change from what it was when it was first added to the Dictionary - so that could be any change in Id , Name , Type , MainColumn or ChildColumns that causes their HashCodes to change. 基于你的代码更新,我的猜测是关于该列的一些内容已经改变,因为它进入字典导致其“实时”哈希代码从它首次添加到字典时的状态改变 - 所以这可能是任何更改导致其HashCodes更改的IdNameTypeMainColumnChildColumns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM