I've been hunting down a bug where I have a dictionary that insists on saying a key does not exist, despite its comparer actually saying that it does. For example, in the following snippet, the exception is thrown:
if (!dictionary.ContainsKey(key))
{
var comparer = dictionary.Comparer;
foreach (var _key in dictionary.Keys)
{
if (comparer.Equals(key, _key) &&
comparer.Equals(_key, key) &&
comparer.GetHashCode(key) == comparer.GetHashCode(_key) &&
comparer.GetHashCode(_key) == comparer.GetHashCode(key))
{
throw new Exception("Key exists, but dictionary doesn't find it");
}
}
}
The dictionary is a generic Dictionary<TKey, TValue>
with the default equality comparer (empty constructor). The TKey
class implements proper GetHashCode
and Equals
methods.
Is there anything I might be missing here? I'm at a complete loss!