As per this solution (https://stackoverflow.com/a/18923091/529618) I am using a ConcurrentDictionary<T,byte>
as a workaround for the lack of ConcurrentHashSet<T>
. However, I'm struggling to see how I can get the original T Key
back out of the dictionary in O(1) time.
var cache = new ConcurrentDictionary<MyEquatableClass, byte>());
//...
if(!cache.TryAdd(classInstance, Byte.MinValue))
return /* Existing cache entry */;
return classInstance;
Is there any way to get the KeyValuePair<K,V>
(or even just the key) for a ConcurrentDictionary<K,V>
entry by giving it an equivalent (IEquatable) key, without enumerating through it in O(n) time?
My problem arises because the objects I'm using as Keys are IEquatable<K>
to one another, but not ReferenceEqual
to one-another. If myDict.ContainsKey(someEquatable)
, I want to get the original key instance in the dictionary (as well as the value stored with it), and throw away my current (duplicate) instance.