Larry
Larry

Reputation: 18041

How to circumvent unused out parameters values?

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?

Upvotes: 0

Views: 1230

Answers (2)

Tim S.
Tim S.

Reputation: 56556

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);
}

Upvotes: 4

alex
alex

Reputation: 12654

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

Upvotes: 7

Related Questions