Represents a collection of associated keys and values.
Object
Dictionary
[Visual Basic] Public Class Dictionary Implements IDictionary, ICollection, IEnumerable, ISerializable, _ IDeserializationEventListener [C#] public class Dictionary : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationEventListener [C++] public __gc class Dictionary : public IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationEventListener [JScript] public class Dictionary implements IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationEventListener
Dictionary is the default implementation of the IDictionary interface.
Each association must have a unique non-null key, but the value of an association can be any object reference, including a null reference (in Visual Basic Nothing). The Dictionary class allows the keys and values to be enumerated, but it does not imply any particular sort order.
The Dictionary class is internally implemented as a hashtable. The Hashtable class inherits from this class.
The objects used as keys in a Dictionary must implement or inherit the GetHashCode and Equals methods. If key equality is simply reference equality, the inherited implementation of these methods would suffice. Furthermore, these methods must produce the same results when called with the same parameters while the key exists in the Dictionary. Key objects must be immutable, as long as they are used as keys in the Dictionary.
When an entry is added to the Dictionary, the entry is placed into a bucket based on the hashcode of the key. Subsequent lookups of the key will use the hashcode of the key to search only in one particular bucket, thus substantially reducing the number of key comparisons required to find an entry.
A Dictionary 's maximum load factor determines the maximum ratio of entries to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default maximum load factor of 1.0 generally provides the best balance between speed and size. A different maximum load factor can also be specified when the Dictionary is instantiated.
As entries are added to a Dictionary, the Dictionary 's actual load factor increases. When the actual load factor reaches the maximum load factor, the number of buckets in the Dictionary is automatically increased to the smallest prime number that is larger than twice the current number of Dictionary buckets.
Each key object in the Dictionary must provide its own hash function, which can be accessed by calling GetHash. However, any object implementing IHashCodeProvider can be passed to a Dictionary constructor, and that hash function would be used for all objects in the table.
Namespace: System.Collections
Assembly: mscorlib.dll
Dictionary Members | System.Collections Namespace | IDictionary | IHashCodeProvider | Hashtable