반응형
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
반응형
'programing tip' 카테고리의 다른 글
(A + B + C) ≠ (A + C + B) 및 컴파일러 재정렬 (0) | 2020.08.02 |
---|---|
Java의 정적 할당-힙, 스택 및 영구 생성 (0) | 2020.08.02 |
JSON.parse 예기치 않은 문자 오류 (0) | 2020.07.30 |
예외 또는 오류 코드에 대한 규칙 (0) | 2020.07.30 |
MongoDB에서 날짜별로 컬렉션을 정렬하는 방법은 무엇입니까? (0) | 2020.07.30 |