Dictionary is returning false on ContainsKey after item is added
- by Karel Frajtak
I have a dictionary declared as follows
IDictionary<string, object> _objectIds = new Dictionary<string, object>();
I was experiencing some problems with it and it discovered that the instance returned false as a result of ContainsKey method and from the watch window I was sure that the item was there.
So I created helper method
private bool IdsContainsKey(string key)
{
lock (syncObject)
{
lock (_objectIds)
{
if (_objectIds.ContainsKey(key))
return true; // A
if (_objectIds.ContainsKey(key))
return true; // B
return _objectIds.ContainsKey(key); // C
}
}
}
During my debugging session I run into situation when the method exited in place B and sometimes I made to C returning true.
Can anybody help me?
Thanks.