programing tip

KeyValuePair VS DictionaryEntry

itbloger 2020. 8. 2. 17:38
반응형

KeyValuePair VS DictionaryEntry


일반 버전 인 KeyValuePair와 DictionaryEntry의 차이점은 무엇입니까?

일반 Dictionary 클래스에서 DictionaryEntry 대신 KeyValuePair가 사용되는 이유는 무엇입니까?


KeyValuePair<TKey,TValue>생성 DictionaryEntry되기 때문에 대신 사용됩니다 . a를 사용 KeyValuePair<TKey,TValue>하면 컴파일러에 사전에있는 정보를 더 많이 제공 할 수 있다는 장점이 있습니다. Chris의 예를 확장하려면 ( <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>는 Dictionary <T, T>를 반복합니다. 이것이 .Net 2 (및 그 이후) 방식입니다.

DictionaryEntry는 HashTables를 반복하기위한 것입니다. 이것이 .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)
{
  // ...
}

참고 URL : https://stackoverflow.com/questions/905424/keyvaluepair-vs-dictionaryentry

반응형