c# - select from Dictionary finds value but select from ConcurrentDictionary does not -


today doing tests concurrentdictionary , dictionary:

class mytest {     public int row { get; private set; }     public int col { get; private set; }     public string value { get; private set; }      public mytest(int row, int col, string value)     {         this.col = col;         this.row = row;         this.value = value;     }       public override bool equals(object obj)     {         mytest other = obj mytest;         return base.equals(other);      }      public override int gethashcode()     {         return (col.gethashcode() ^ row.gethashcode() ^ value.gethashcode());     }  } 

using entity above created , filled concurrentdictionary , dictionary , tried code below:

    concurrentdictionary<mytest, list<mytest>> _test = new concurrentdictionary<mytest, list<mytest>>();     dictionary<mytest, list<mytest>> _test2 = new dictionary<mytest, list<mytest>>();          mytest dunno = _test.values.asparallel().select(x => x.find(a => a.col == 1 && a.row == 1)).firstordefault();         mytest dunno2 = _test2.values.asparallel().select(x => x.find(a => a.col == 1 && a.row == 1)).firstordefault(); 

the first 1 returns value second 1 not, doing wrong?

this code used add values:

            _test.addorupdate(cell10,             new list<mytest>             {                 new mytest(1, 1, "ovpsomevaluevalue"),                 new mytest(1, 2, "ocpsomevaluevalue")             },             (key, value) => value = new list<mytest>());          _test2.add(cell10,             new list<mytest>             {                 new mytest(1, 1, "ovpsomevaluevalue"),                 new mytest(1, 2, "ocpsomevaluevalue")             }             ); 

you calling addorupdate, , third parameter should create new empty list, result end empty list. guessing somewhere before line of code adding entry same key.

also notice equals function incorrect. comparing on reference, not on actual values use in gethashcode. suggest use default template overriding equals:

     protected bool equals(x other) {         return row == other.row && col == other.col && string.equals(value, other.value);      }       public override bool equals(object obj)      {         if (referenceequals(null, obj)) return false;         if (referenceequals(this, obj)) return true;         if (obj.gettype() != this.gettype()) return false;         return equals((x) obj);      }       public override int gethashcode()      {         unchecked         {            var hashcode = row;            hashcode = (hashcode*397) ^ col;            hashcode = (hashcode*397) ^ (value != null ? value.gethashcode() : 0);            return hashcode;         }      } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -