1

I would like to initialize value to 0 if it does not already exists. Otherwise it should increment existing value.

ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>();
dic.AddOrUpdate(1, 0, (key, old) => old++);
dic.AddOrUpdate(2, 0, (key, old) => old++);

At this point, dictionary has keys of 1 and 2 with value of 0 each.

        dic.AddOrUpdate(1, 0, (key, old) => old++);

At this point for the key 1 the value should be 1 whereas for key 2 it should be 0, however, both have value of 0. Any idea why?

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
paul deter
  • 857
  • 2
  • 7
  • 20

2 Answers2

5

You have a misconception:

dic.AddOrUpdate(1, 0, (key, old) => old++);

At this point for the key 1 the value should be 1

when you use old++ it returns the original value before the modification to be stored. It is as if you did the equivalent of:

dic.AddOrUpdate(1, 0, (key, old) => 
{
    var original = old;
    old = old + 1;
    return original;
});

You want ++old which will return the modified value or just use

dic.AddOrUpdate(1, 0, (key, old) => old + 1);
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

Try this :

dic.AddOrUpdate(1, 0, (key, old) => old + 1);

I think that's because old is a parameter of the Func<>, it cant be modified.

rducom
  • 7,072
  • 1
  • 25
  • 39
  • 1
    It's not that old cannot be modified it's that `old++` returns the value before the increment when you want the value after. So `++old` would also work. – juharr Jan 29 '15 at 16:29
  • You can modify `old`, it's just that it doesn't *accomplish* anything. The value will be set to whatever the lambda returns, not what the value of `old` is at the end of the method. – Servy Jan 29 '15 at 16:42