Please add a ConcurrentDictionary.CompareAndRemove method RRS feed

  • General discussion

  • I very sorely miss a method on the ConcurrentDictionary, a kind of CompareAndRemove. In my experience, this is the reason why I usually stop using the ConcurrentDictionary, and switch back to an ordinary one accessed under lock.

    The method TryRemove will remove something, only after which I may test and see if the removed item is the one I expected there. I can then put it back, but this created a period of time in which the item was missing. Other threads then must be able to handle this situation, needlessly complicating my code or completely eliminating the usability of the ConcurrentDictionary. This really happened to me a lot of times by now.

    I have found an article once which states that casting the ConcurrentDictionary into an ICollection<KeyValuePair<TKey, TValue>> and then using the Remove method from that interface will actually give this exact functionality. However, this is obscure, and it is to this day still undocumented in the MSDN, so I do not feel safe relying on it. (NB that Mono, OTOH, does not implement that method in this way. Mono will only use the key and remove anything that is there.)

    I would really be grateful if the ConcurrentDictionary had a CompareAndRemove method.

    I am curious, do other people feel this issue? Please share you thoughts.

    Thursday, June 5, 2014 8:43 AM

All replies