字典是使用自定义的关键,但关键是始终不平等 [英] Dictionary using is custom key but key is always unequal

查看:159
本文介绍了字典是使用自定义的关键,但关键是始终不平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RTBTextPointer在字典定义按键...

I am using RTBTextPointer as custom key in dictionary...

   Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary<RTBTextPointer, SpintaxEditorProperties.SpintaxMappedValue>(new RTBTextPointerComparer());



我又写道这个RTBTextPointer和RTBTextPointerComparer类类库,并在不同的WPF项目使用此,

I worte this RTBTextPointer, and RTBTextPointerComparer classes in class library and using this in different wpf projects,

 if (Init.SpintaxEditorPropertyMain.SpintaxListDict.ContainsKey(_index) == false)
        {
            Init.SpintaxEditorPropertyMain.SpintaxListDict.Add(_index,_SpintaxMappedVal);
        }



每次的containsKey返回false,即使它含有,所以在字典中会出现重复的条目。 。什么错在我的的GetHashCode()

everytime containsKey returns false, even it contains, so duplication entry occurs in dictionary.. is anything wrong in my "GetHashCode()"

    public class RTBTextPointer
    {
    static int _row;
    static int _column;

    public int Row
    {
        get
        {
            return _row;
        }
        set
        {
            _row = value;
        }
    }
    public int Column
    {
        get
        {
            return _column;
        }
        set
        {
            _column = value;
        }
    }

}

public class RTBTextPointerComparer : IEqualityComparer<RTBTextPointer>
{
    public bool Equals(RTBTextPointer x, RTBTextPointer y)
    {         
        bool result = int.Equals(x.Column, y.Column) && (int.Equals(x.Row, y.Row));

        return result;
    }

    public int GetHashCode(RTBTextPointer obj)
    {
        var result = 0;
        int hCode = obj.Column ^ obj.Row;
        result = hCode.GetHashCode();
        return result;
    }
}

请帮我
预先感谢

Please help me Thanks in advance

推荐答案

我不认为你需要创建一个独立的比较器。只是覆盖等于的GetHashCode 就足够了。

I don't think you need to create a separate comparer. Just overriding Equals and GetHashCode should suffice.

另外,如果你有非常简单的属性那样,你可以切换到的自动属性

Also, if you have very simple properties like that, you could switch to auto properties

public class RTBTextPointer
{
    public int Row
    {
        get;
        set;
    }
    public int Column
    {
        get;
        set;
    }
    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        var other = obj as RTBTextPointer;
        if (other == null)
        {
            return false;
        }
        return other.Row == Row && other.Column == Column;
    }
    public override int GetHashCode()
    {
        unchecked
        {
            // 397 or some other prime number
            return (Row * 397) ^ Column;
        }
    }
}

请参阅的选中,了解有关的更多信息。

See unchecked for more information about that.

如果有两个以上的特性,如果这些属性可以为空,则的GetHashCode 可能是这样的:

If you have more than two properties, and if those properties could be null, the GetHashCode might look like this:

unchecked
{
    var result = 0;
    result = (result * 397) ^ (Prop1 != null ? Prop1.GetHashCode() : 0);
    result = (result * 397) ^ (Prop2 != null ? Prop2.GetHashCode() : 0);
    result = (result * 397) ^ (Prop3 != null ? Prop3.GetHashCode() : 0);
    result = (result * 397) ^ (Prop4 != null ? Prop4.GetHashCode() : 0);
    // ...
    return result;
}

这篇关于字典是使用自定义的关键,但关键是始终不平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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