Search code examples
c#collectionsconcurrencyconcurrentdictionary

Under what cirumstances does ConcurrentDictionary.TryRemove() return false?


I've been searching for a way to remove an item from a concurrent dictionary. For some reason there's no Remove method, but there's a TryRemove method, that returns a bool. In MSDN it's said that false is returned in case the process "failed", but doesn't say in what way.

  1. Is it false when the element is absent only?
  2. Is it false when the element couldn't be removed due to a lock?

Solution

  • 1st option only: when there's no key to remove; locking is ConcurrentDictionary internal implementation and it should not be visible from outside.

    According to comment by Dmitry Bychenko.