Concurrent Dictionary AddOrUpdate vs Index Add

前端 未结 4 562
走了就别回头了
走了就别回头了 2021-02-03 21:06

There are two ways I\'ve assigned values to a existing key in a concurrent dictionary in my current project.

A. concurrentDictionary1[key] = value;

4条回答
  •  时光取名叫无心
    2021-02-03 21:47

    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.

提交回复
热议问题