问题描述
这是通用版本的DictionaryEntry KeyValuePair之间的区别是什么?
What is the difference between KeyValuePair which is the generic version and DictionaryEntry?
为什么KeyValuePair来代替的DictionaryEntry的泛型类Dictionary?
Why KeyValuePair is used instead of DictionaryEntry in generic Dictionary class?
推荐答案
KeyValuePair< TKEY的,TValue>
代替使用的DictionaryEntry
,因为它是泛型。使用 KeyValuePair 1所述的优势; TKEY的,TValue>
的是,我们可以给编译器有什么是我们的字典详细信息。为了扩大克里斯的例子(在此我们有一个包含两个字典<字符串,整数>
对)。
KeyValuePair<TKey,TValue>
is used in place of DictionaryEntry
because it is generified. The advantage of using a KeyValuePair<TKey,TValue>
is that we can give the compiler more information about what is in our dictionary. To expand on Chris' example (in which we have two dictionaries containing <string, int>
pairs).
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 VS的DictionaryEntry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!