Respuestas:
KeyValuePair<TKey,TValue>se usa en lugar de DictionaryEntryporque está genérico. La ventaja de usar un KeyValuePair<TKey,TValue>es que podemos darle al compilador más información sobre lo que hay en nuestro diccionario. Para ampliar el ejemplo de Chris (en el que tenemos dos diccionarios que contienen <string, int>pares).
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in dict) {
int i = item.Value;
}
Hashtable hashtable = new Hashtable();
foreach (DictionaryEntry item in hashtable) {
// Cast required because compiler doesn't know it's a <string, int> pair.
int i = (int) item.Value;
}
KeyValuePair <T, T> es para iterar a través del Dictionary <T, T>. Esta es la forma .Net 2 (y posteriores) de hacer las cosas.
DictionaryEntry es para iterar a través de HashTables. Esta es la forma .Net 1 de hacer las cosas.
He aquí un ejemplo:
Dictionary<string, int> MyDictionary = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in MyDictionary)
{
// ...
}
Hashtable MyHashtable = new Hashtable();
foreach (DictionaryEntry item in MyHashtable)
{
// ...
}