जवाबों:
KeyValuePair<TKey,TValue>के स्थान पर उपयोग किया जाता है DictionaryEntryक्योंकि यह उत्पन्न होता है। एक का उपयोग करने का लाभ यह KeyValuePair<TKey,TValue>है कि हम संकलक को हमारे शब्दकोश में क्या है के बारे में अधिक जानकारी दे सकते हैं। क्रिस के उदाहरण पर विस्तार करने के लिए (जिसमें हमारे पास दो शब्दकोश युक्त <string, int>जोड़े हैं)।
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> शब्दकोश <T, T> के माध्यम से पुनरावृति के लिए है। यह .Net 2 (और आगे) चीजों को करने का तरीका है।
DictionaryEntry हैशटेबल्स के माध्यम से पुनरावृत्ति के लिए है। यह .Net 1 काम करने का तरीका है।
यहाँ एक उदाहरण है:
Dictionary<string, int> MyDictionary = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in MyDictionary)
{
// ...
}
Hashtable MyHashtable = new Hashtable();
foreach (DictionaryEntry item in MyHashtable)
{
// ...
}