0

I am trying to use the AddOrUpdate() method in the ConcurrentDictionary class. I am having trouble with the Update peice. The following is the code:

dictionary.AddOrUpdate(oid, pList, (o,p) =>
{
    for (int i=0; i < p.Count; i++)
        p[i] = pList[i];
    return p;
});

I would like to update the 3 values contained in a pList object when/if I hit the dictionary with a key that is already present. At the moment, only the very last for iteration ( p[2]) is being updated in the dictionary. What am I missing?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Walter Kelt
  • 2,199
  • 1
  • 18
  • 22
  • Sounds similar to this issue http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx does it work ok if you just return pList or create a a new list and return that? – MaximR Nov 02 '12 at 00:36
  • exact same behavior, only the last item in the list gets updated. The other 2 stay the same. – Walter Kelt Nov 02 '12 at 00:58
  • I found an error in the code generating plist. All I needed was (o,p)=>pList – Walter Kelt Nov 02 '12 at 03:08

0 Answers0