[Mono-dev] Key matching issue in generic Dictionary

Generic 2006 generic2006 at libero.it
Wed Aug 23 09:04:02 EDT 2006


Hi all

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, with the following result. A
System.Collections.Generic.Dictionary instance is incapsulated in
MyDictionary (an implementation of generic IDictionary) , TKey is
assigned to Name type, TValue is irrelevant. Name has this constructor:
Name(string).

class MyDictionary
{

 public bool ContainsKey(Name key)
 {
   Console.WriteLine("PdfDictionary.ContainsKey: key = " + key.Value);
   return this.entries.ContainsKey(key);
 }
}

1) I populate MyDictionary with 3 entry pairs, whose keys are:
Name("Info"), Name("Root"), Name("Size");
2) I try to find Name("Encrypt") among MyDictionary keys, using
MyDictionary.ContainsKey(Name key) and obtaining such console output:
MyDictionary.ContainsKey: key = Encrypt // entered the
MyDictionary.ContainsKey(Name key) method searching Name("Encrypt");
//
Name.Equals: this: Encrypt other: Info
Name.Equals: this: Encrypt other: Root

PdfDictionary[]: key = Size
PdfDictionary.ContainsKey: key = Root
PdfName.Equals: this: Root other: Size
PdfDictionary[]: contains key = Size: False
PdfDictionary[]:exception: The given key was not present in the dictionary.
PdfDictionary[]: value = null; key = Size
PdfName.Equals: this: Size other: Info
PdfDictionary[]: pair: key = Info equals : False hashcode: -26085888
PdfName.Equals: this: Size other: Root
PdfDictionary[]: pair: key = Root equals : False hashcode: 904636160
PdfName.Equals: this: Size other: Size
PdfDictionary[]: pair: key = Size equals : True hashcode: -1994248064
ReadXRefTable:1.1: trailer count:3




More information about the Mono-devel-list mailing list