KeyValuePair<T,T>
is used in place of DictionaryEntry because it is generified. The advantage of using a KeyValuePair<T,T>
is that we can give the compiler more information about what is in our dictionary. To expand on Chris' example (in which we have two dictionaries containing <string, int>
pairs).
Dictionary<string, int> MyDictionary = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in MyDictionary)
{
int i = item.Value;
}
Hashtable MyHashtable = new Hashtable();
foreach (DictionaryEntry item in MyHashtable)
{
// Cast required because compiler doesn't know it's a <string, int> pair.
int i = (int) item.Value;
}