I was looking for a method in ConcurrentDictionary that allows me to delete a record by key, if and only if the value is equal to the one I specify, something like TryUpdate equivalent, but for deletion.
The only method that does this is this method:
ICollection<KeyValuePair<K, V>>.Remove(KeyValuePair<K, V> keyValuePair)
This is an explicit implementation of the ICollection interface, in other words, I must first pass my ConcurrentDictionary to ICollection so that I can call Remove.
Delete does exactly what I want, and that casting doesn't matter much, also the source code shows that it calls the private TryRemovalInternal method with bool matchValue = true, so everything looks beautiful and clean.
What bothers me a bit is the fact that it is not documented as an optimistic parallel Remove ConcurrentDictionary method, so http://msdn.microsoft.com/en-us/library/dd287153.aspx just duplicates the ICollection pattern, and How to add and remove items from ConcurrentDictionary also does not mention this method.
Does anyone know if this can go, or is there some other method that I am missing?
Eugene beresovsky
source share