Search code examples
c#dictionarygethashcodeiequalitycomparercontainskey

Dictionary using is custom key but key is always unequal


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

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

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

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


Solution

  • 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.

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