Why does ConcurrentDictionary.TryRemove require a second out argument?

后端 未结 4 1721
执笔经年
执笔经年 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:36

    If you're not interested in the value that was removed, simply call IDictionary.Remove(key). It's shadowed, so you have to invoke it explicitly.

    Example:

    var dict = new ConcurrentDictionary();
    dict.AddOrUpdate("mykey", (val) => "test", (val1, val2) => "test");
    ((IDictionary)dict).Remove("mykey");
    

    The TryRemove(key, out value) method is there to give you feedback whether the operation made any change. Use the one that best suits your needs.

提交回复
热议问题