Why does ConcurrentDictionary.TryRemove require a second out argument?

后端 未结 4 1710
执笔经年
执笔经年 2020-12-15 15:03

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 h

4条回答
  •  有刺的猬
    2020-12-15 15:40

    You can create exactly the method you want:

    public static class ConcurrentDictionaryEx {
      public static bool TryRemove(
        this ConcurrentDictionary self, TKey key) {
        TValue ignored;
        return self.TryRemove(key, out ignored);
      }
    }
    

    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, which leads you back to creating an extension method if you want to avoid casting a ConcurrentDictionary reference:

    public static class ConcurrentDictionaryEx {
      public static bool Remove(
        this ConcurrentDictionary self, TKey key) {
          return ((IDictionary)self).Remove(key);
      }
    }
    

提交回复
热议问题