Concurrent Dictionary AddOrUpdate vs Index Add

c#

Solution

They both call `TryAddInternal`, so behave exactly the same**.

Update:

There is another difference. Here's the code from the indexer:

set
{
    TValue tValue;
    if (key == null)
    {
        throw new ArgumentNullException("key");
    }
    this.TryAddInternal(key, value, true, true, out tValue);
}

vs. from the method

while (!this.TryAddInternal(key, tValue, false, true, out tValue1));

So it appears there is a chance that the indexer will fail silently, whereas the method will continue trying until it succeeds. Hmm, some more in-depth analysis would be required to fully understand the differences between the two :/

Decompilers are still your friend.

**I derped.

Problem

There are two ways I've assigned values to a existing key in a concurrent dictionary in my current project. A. `concurrentDictionary1[key] = value`; and B. `concurrentDictionary2.AddOrUpdate(key, value, (k, v) => value);` If I know that the 'key' exists, are these functionally equivalent? Is protection offered by the concurrency of the Concurrent Dictionary bypassed with method 'A'? What is the difference here? What are the reasons for choosing one over the other? I looked through the documentation at msdn, and it seems they only initialize a concurrent dictionary with method 'A', not update it.

Original source