简体   繁体   中英

How to remove from ConcurrentDictionary if i dont want to get the value

it seems that ConcurrentDictionary only has a TryRemove method but no Remove method. Why is this?

I don't care about the value when i want to remove an entry by key. how can i do this?

TryRemove is native method of ConcurrentDictionary , whereas like with other collections, you do have an explicit implementation of ICollection with Remove method available:

ConcurrentDictionary<TKey, TValue>.ICollection<KeyValuePair<TKey, TValue>>.Remove

http://msdn.microsoft.com/en-us/library/dd287153%28v=vs.110%29.aspx

So TryRemove is a safe alternative to Remove , but you can choose between the two.

As far as I remember, ConcurrentDictionary is for use in multiple threads scenarios.

The lack of "Remove", and the existence of "TryRemove" is because two different threads may attempt to remove a specific item from the collection at the same time, but for obvious reasons only one can do it.

TryRemove tries to remove the item, but if the item is not found, no exception will happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM