BCL Generics—Dictionary TKey, TValue


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

BCL Generics

© 2005 Wiley Publishing, Inc.

Dictionary<TKey, TValue>

The Dictionary<TKey, TValue> class is used to map keys to values in a collection. These keys, each of which must be unique, will then allow you randomly access the values that are stored in the collection. As you can imagine, the efficiency and general behavior of the Dictionary<TKey, TValue> class makes it a priceless tool that has countless applications.

If you’ve been working with the System.Collections namespace, you’ve probably already been using the HashTable class. In the generics namespace, the Dictionary<TKey, TValue> class is the functional equivalent of the HashTable. However, even though it inherits most of the concepts from the HashTable, it represents a mostly revamped implementation. As such, its performance characteristics will not necessarily match those of the HashTable.

The following table summarizes the methods and properties that are part of the Dictionary<TKey, TValue> class.


Method or Property Name Description
Add() Adds the supplied key and value to the dictionary.
Clear() Removes all the items from the dictionary.
ContainsKey() Determines if the supplied key exists in the current dictionary, returning true if it is found.
ContainsValue() Determines if the supplied value exists in the current dictionary, returning true if it is found.
Count Returns the number of items currently stored in the dictionary.
GetEnumerator() Returns an enumerated collection of KeyValuePair objects corresponding to the items in the dictionary.
GetObjectData() Retrieves a serialized representation of the dictionary.
Item Indexer property that uses keys to add or update items in the dictionary.
Keys Returns a Dictionary.KeyCollection containing all the keys in the dictionary.
OnDeserialization() Called when deserialization has completed.
Remove() Finds the items that match the passed-in key and removes it. If the item is found, the method returns true.
TryGetValue() Attempts to get the value that corresponds to the supplied key. If the value is found, it is returned. Otherwise, the default value for the value parameter is returned.
Values Returns a Dictionary.ValuesCollection containing all the values in the dictionary.


Previous_Page_.gif Next_Page_.gif


Personal tools