Dictionary.ContainsKey misbehave c# [英] Dictionary.ContainsKey misbehave c#

查看:105
本文介绍了Dictionary.ContainsKey misbehave c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类

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. When I typed

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

我有 false

地球上的是?

I got false.
How on Earth can it be?

更新

我的类有GetHashCode或Equals功能。这些是他们的实现:

UPDATED
my Column class has GetHashCode or 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.
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.

其背景如下:
字典在添加时检索和存储密钥的哈希码。如果密钥的哈希码在此之后更改,则与存储的哈希码不同,并且对象不会被视为等于最初插入的密钥。

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.

这篇关于Dictionary.ContainsKey misbehave c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆