title: How to achieve remove_if functionality in .NET ConcurrentDictionary
body: <p>I have a scenario where I have to keep reference counted object for given key in the <code>ConcurrentDictionary</code>, if reference count reaches <code>0</code>, I want to delete the key. This has to be thread safe hence I am planning to use the <code>ConcurrentDictionary</code>.</p>
  
  <p>Sample program as follows. In the concurrent dictionary, I have key and value , the value is KeyValuePair which holds my custom object and reference count.</p>
  
  <pre><code>ConcurrentDictionary&lt;string, KeyValuePair&lt;object, int&gt;&gt; ccd = 
      new ConcurrentDictionary&lt;string, KeyValuePair&lt;object, int&gt;&gt;();
  
  // following code adds the key, if not exists with reference 
  // count   for  my custom object to 1
  // if the key already exists it increments the reference count
  
  var addOrUpdateValue = ccd.AddOrUpdate("mykey",
      new KeyValuePair&lt;object, int&gt;(new object(), 1),
      (k, pair) =&gt; new KeyValuePair&lt;object, int&gt;(pair.Key, pair.Value + 1));
  </code></pre>
  
  <p>Now I want a way to remove the key when the reference count reaches to 0. I was thinking , remove method on <code>ConcurrentDictionary</code> which takes key and predicate , removes the key if the predicate return 'true'. Example.</p>
  
  <pre><code>ConcurrentDictionary.remove(TKey, Predicate&lt;TValue&gt; ). 
  </code></pre>
  
  <p>There is no such method on <code>ConcurrentDictionary</code>, question is how to do the same in thread safe way ?.</p>