0

I am using ConcurrentDictionary, and I need to remove elements using TryRemove.

The implementation of TryRemove requires an out parameter that returns the object that will be removed.

Body dummy;
if (!bulletBodies.TryRemove(bullet, out dummy))
{

}
...

I do not want to use an additional line on a dummy variable whenever I delete an entry in my dictionary: that's why I tried to circumvent the out return parameter unsuccesfully typing the horrors below. I also googled a bit, without success.

bulletBodies.TryRemove(bullet, null);
bulletBodies.TryRemove(bullet, out null);
bulletBodies.TryRemove(bullet, void);
...

Is there a way or a clever tip to manage unused out parameters, so there is no need to declare dummy variables anywhere?

Larry
  • 17,605
  • 9
  • 77
  • 106

2 Answers2

7

You can create an extension method TryRemove that does not return removed value.

alex
  • 12,464
  • 3
  • 46
  • 67
4

You could write an extension method, such as:

public static bool TryRemove<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key)
{
    TValue dummy;
    return dictionary.TryRemove(key, out dummy);
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122