[Mono-dev] Key matching issue in generic Dictionary [correct message]

Brian Crowell mono-devel at fluggo.com
Wed Aug 23 10:35:37 EDT 2006


Generic 2006 wrote:
> I'm facing a strange behavior affecting System.Collections.Generic.Dictionary<TKey,TValue>: when I try to search for a key among the dictionary entries using interface members such as ContainsKey(<TKey> key) or this[<TKey> key] I receive wrong responses (the key can't be find, whilst it's really there!).
> I supposed that it could be a problem on the TKey side (where I implemented System.IEquatable<TKey>), so I put some Console.WriteLine()
> inside the code to verify. A System.Collections.Generic.Dictionary instance is incapsulated in MyDictionary (an implementation of generic IDictionary), TKey is assigned to Name type, TValue is irrelevant.

IEquatable<T> is not enough. You need to override Object.GetHashCode() and 
Object.Equals(). IEquatable<T> is optional, GetHashCode and Equals are mandatory.

--Brian



More information about the Mono-devel-list mailing list