Reputation: 1899
Is approach below thread safe? I know ConcurrentDictionary provides TryRemove method but I just don't like to have a variable for value that I never gonna use.
ConcurrentDictionary<int, int> myDict = new ConcurrentDictionary<int, int>();
if (myDict.ContainsKey(1))
((IDictionary<int, int>)myDict).Remove(1);
Upvotes: 0
Views: 79
Reputation: 171178
Your aim seems to be to improve code clarity by removing the need to use an out
parameter. That is a valid concern but using Remove
is not the right way to go.
Define yourself a helper method:
static bool TryRemove<TKey, Value>(
this ConcurrentDictionary<TKey, Value> dict, TKey key) {
TValue value;
return dict.TryRemove(key, out value);
}
Upvotes: 4