C# Dictionary 的几种遍历方法

 Dictionary<string,int> list =newDictionary<string,int>();
  list.Add("d", 1);

//3.0以上版本

           foreach(variteminlist)

            {

               Console.WriteLine(item.Key + item.Value);

            }

           //KeyValuePair<T,K>

           foreach(KeyValuePair<string,int> kvinlist)

            {

               Console.WriteLine(kv.Key + kv.Value);

            }

           //过键的集合取

           foreach(stringkeyinlist.Keys)

            {

               Console.WriteLine(key + list[key]);

            }

           //直接取值

           foreach(intvalinlist.Values)

            {

               Console.WriteLine(val);

            }

            //非要采用for的方法也可

           List<string> test =newList<string>(list.Keys);

 

           for(inti = 0; i < list.Count; i++)

            {

               Console.WriteLine(test[i] + list[test[i]]);

            }

 

内容源自 http://wutaichi.blog.sohu.com/190336531.html

posted @ 2013-05-16 14:51  J.Y  阅读(271)  评论(0编辑  收藏  举报