问题描述
我只希望可以删除该值。我不需要事后使用变量。为什么不包括在这里是不需要第二个参数的重载?
I only want to remove a value.. I don't need to use the variable afterwards. Why not include an overload where this second parameter was not required?
难道我真的只是存储在一个临时的局部变量,而不是使用它,并有垃圾收藏家收集它,当方法结束?似乎是相当愚蠢的。
Do I really have to just store it in a temporary local variable, not use it, and have the garbage collector collect it when the method ends? Seems rather silly..
功能:
推荐答案
您可以创建正是你想要的方式:
You can create exactly the method you want:
public static class ConcurrentDictionaryEx {
public static bool TryRemove<TKey, TValue>(
this ConcurrentDictionary<TKey, TValue> self, TKey key) {
TValue ignored;
return self.TryRemove(key, out ignored);
}
}
更新:或者,作为,只是使用的。但需要注意的是,因为它是一个显式接口实现,你需要参考的的IDictionary< TKEY的,TValue>
,从而导致你回到创建扩展方法,如果您要避免铸造 ConcurrentDictionary< TKEY的,TValue>
引用:
UPDATE: Or, as Dialecticus mentioned in the comments, just use Remove
. But note that, since it's an explicit interface implementation, you'll need a reference to an IDictionary<TKey, TValue>
, which leads you back to creating an extension method if you want to avoid casting a ConcurrentDictionary<TKey, TValue>
reference:
public static class ConcurrentDictionaryEx {
public static bool Remove<TKey, TValue>(
this ConcurrentDictionary<TKey, TValue> self, TKey key) {
return ((IDictionary<TKey, TValue>)self).Remove(key);
}
}
这篇关于为什么ConcurrentDictionary.TryRemove需要第二个说法出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!