Question: When does ConcurrentDictionary<TKey, TValue>.TryRemove(TKey, out TValue)
#97994
-
I'm trying to use a concurrent dictionary for thread-safe access, and although it's not the main concern, high performance and low code complexity are something I'm interested in. Looking at the documentation for ConcurrentDictionary<TKey, TValue>.TryRemove(TKey, out TValue), The comment about the return value is " What does "removed successfully" mean? Does it mean the key was found, or is it possible for a key to exist while being failed to be removed? It's not inherently clear to me from this phrasing. Right now I'm being defensive and writing code like this: internal void Clear(string context)
{
while (_outputs.ContainsKey(context))
{
if (_outputs.TryRemove(context, out StringWriter? writer))
{
writer.Dispose();
}
}
} And I want to know if it's still safe to write internal void Clear(string context)
{
if (_outputs.TryRemove(context, out StringWriter? writer))
{
writer.Dispose();
}
} If possible, could the documentation be changed so its more clear to anyone not familiar enough with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsI'm trying to use a concurrent dictionary for thread-safe access, and although it's not the main concern, high performance and low code complexity are something I'm interested in. Looking at the documentation for ConcurrentDictionary<TKey, TValue>.TryRemove(TKey, out TValue), The comment about the return value is " What does "removed successfully" mean? Does it mean the key was found, or is it possible for a key to exist while being failed to be removed? It's not inherently clear to me from this phrasing. Right now I'm being defensive and writing code like this: internal void Clear(string context)
{
while (_outputs.ContainsKey(context))
{
if (_outputs.TryRemove(context, out StringWriter? writer))
{
writer.Dispose();
}
}
} And I want to know if it's still safe to write internal void Clear(string context)
{
if (_outputs.TryRemove(context, out StringWriter? writer))
{
writer.Dispose();
}
} If possible, could the documentation be changed so its more clear to anyone not familiar enough with
|
Beta Was this translation helpful? Give feedback.
If it exists, it'll be found and removed and true will be returned.