Respostas:
KeyValuePair<TKey,TValue>é usado no lugar de DictionaryEntryporque é generificado. A vantagem de usar um KeyValuePair<TKey,TValue>é que podemos fornecer ao compilador mais informações sobre o que está em nosso dicionário. Para expandir o exemplo de Chris (no qual temos dois dicionários contendo <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> é para iteração por meio do Dicionário <T, T>. Esta é a maneira .Net 2 (e mais adiante) de fazer as coisas.
DictionaryEntry é para iterar por meio de HashTables. Esta é a maneira .Net 1 de fazer as coisas.
Aqui está um exemplo:
Dictionary<string, int> MyDictionary = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in MyDictionary)
{
// ...
}
Hashtable MyHashtable = new Hashtable();
foreach (DictionaryEntry item in MyHashtable)
{
// ...
}