哈希表和Dictionary泛型的循环方式

在我们平时的开发当中,经常会遇到<键,值>这种搭配

 

在泛型没出的时候我们用的是HashTable

 

它的循环方式如下:

HashTable ht = new HashTable();

foreach(DictionaryEntry xx in ht)

{

     Response.Write(xx.Key);  

     Response.Write(xx.Value);

}

 

有了泛型字典我们就可以这样用

 

Dictionary<string,string> dt = new Dictionary<string,string>();

foreach(KeyValuePair<string,string> xx in dt)

{

     Response.Write(xx.Key);  

     Response.Write(xx.Value);

}

 

虽然功能差不多,但Dictionary 继承了IEnumerator接口,提供了更多泛型特有的方法给我们用,具体的大家用Reflector反编译一下看下源代码了

 

posted @ 2009-12-27 22:06  songzibin  阅读(1377)  评论(0编辑  收藏  举报