码迷,mamicode.com
首页 > Windows程序 > 详细

C# Dictionary 的几种遍历方法 转

时间:2020-01-25 21:58:28      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:int   line   ons   集合   values   air   nbsp   val   版本   

  Dictionary<string, int> list = new Dictionary<string, int>();

 

            list.Add("d", 1);

 

            //3.0以上版本

            foreach (var item in list)

            {

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

            }

            //KeyValuePair<T,K>

            foreach (KeyValuePair<string, int> kv in list)

            {

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

            }

            //通过键的集合取

            foreach (string key in list.Keys)

            {

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

            }

            //直接取值

            foreach (int val in list.Values)

            {

                Console.WriteLine(val);

            } 

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

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

 

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

            {

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

            }

转 自 https://www.cnblogs.com/cjdxhc_site/articles/1727763.html

C# Dictionary 的几种遍历方法 转

标签:int   line   ons   集合   values   air   nbsp   val   版本   

原文地址:https://www.cnblogs.com/enych/p/12233424.html

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