A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© .net_交流 中级黑马   /  2014-7-24 22:17  /  1057 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 .net_交流 于 2014-7-26 23:19 编辑

dictionary 的遍历除了用keyvaluepair 还有其他什么方法吗??

评分

参与人数 1技术分 +1 收起 理由
陈君 + 1

查看全部评分

5 个回复

倒序浏览
路过,我也只知道通过通过KeyValuePair来遍历元素。看哪个高手会
回复 使用道具 举报
foreach也可以
回复 使用道具 举报 0 1
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 + list[test]);
            }

评分

参与人数 1技术分 +1 收起 理由
陈君 + 1

查看全部评分

回复 使用道具 举报
天佑の清清 发表于 2014-7-25 11:14
Dictionary list = new Dictionary();
            list.Add("d", 1);
            //3.0以上版本

谢谢大神回答,太完美了!!!!
回复 使用道具 举报
大神果然厉害啊!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马