码迷,mamicode.com
首页 > 其他好文 > 详细

KeyValuePair VS DictionaryEntry

时间:2016-06-27 19:50:10      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

There are some difference between KeyValuePair which is the generic version and DictionaryEntry which is non generic version. 

  1. KeyValuePair < T,T > is for iterating through Dictionary < T,T >. This is the .Net 2 way of doing things.
  2. DictionaryEntry is for iterating through HashTables. This is the .Net 1 way of doing things.
  3. KeyValuePair<TKey,TValue> is used in place of DictionaryEntry because it is generic.
The advantage of using a KeyValuePair<TKey,TValue> is that we can give the compiler more information about what is in our dictionary. Esp. the data type
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

标签:

原文地址:http://www.cnblogs.com/dennysong/p/5621277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!