'C# Dictionary: Java HashMap.compute() equivalent in C# Dictionary?

Java's HashMap.compute() method is very powerful, in that it can:

  1. add a (key, value) pair, if key was not previously present
  2. update value through a lambda (via its return), if key was previously present
  3. remove key from the hashmap, if the supplied lambda returns null - user doesn't need to do HashMap.remove(key) (which is one extra hashmap lookup)

C++'s unordered_map relies on the iterator returned from the find method, and uses erase(iterator) to avoid extra lookup

Rust's HashMap provides the Entry API, which uses the Entry::remove method to delete the entry without extra lookup

However, I don't seem to find similar facility in C#'s Dictionary API - could anyone shed some light on the design of C#'s Dictionary API?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source