c# - Dictionary using is custom key but key is always unequal -


i using rtbtextpointer custom key in dictionary...

   init.spintaxeditorpropertymain.spintaxlistdict = new dictionary<rtbtextpointer, spintaxeditorproperties.spintaxmappedvalue>(new rtbtextpointercomparer()); 

i worte rtbtextpointer, , rtbtextpointercomparer classes in class library , using in different wpf projects,

 if (init.spintaxeditorpropertymain.spintaxlistdict.containskey(_index) == false)         {             init.spintaxeditorpropertymain.spintaxlistdict.add(_index,_spintaxmappedval);         } 

everytime containskey returns false, contains, duplication entry occurs in dictionary.. wrong in "gethashcode()"

    public class rtbtextpointer     {     static int _row;     static int _column;      public int row     {                 {             return _row;         }         set         {             _row = value;         }     }     public int column     {                 {             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 me in advance

i don't think need create separate comparer. overriding equals , gethashcode should suffice.

also, if have simple properties that, switch 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 rtbtextpointer;         if (other == null)         {             return false;         }         return other.row == row && other.column == column;     }     public override int gethashcode()     {         unchecked         {             // 397 or other prime number             return (row * 397) ^ column;         }     } } 

see unchecked more information that.

if have more 2 properties, , if properties null, gethashcode might 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; } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -