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

C# Dictionary 应用

时间:2015-03-12 15:01:26      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

1、字典定义并添加数据

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("a", "北京");
            dic.Add("c", "上海");
            dic.Add("b", "广州");
            dic.Add("d", "深圳");

2、遍历字典项

            foreach (KeyValuePair<string, string> d in dic)
            {
                Console.WriteLine(d.Key + "  " + d.Value);
            }
            foreach (var itm in dic)
            {
                Console.WriteLine(itm.Key + "  " + itm.Value);
            }
            foreach (string s in dic.Keys)
            {
                Console.WriteLine(s + "  " + dic[s]);
            }

            foreach (string s in dic.Values)
            {
                Console.WriteLine(s);
            }

3、字典排序并输出

            字典排序:List<KeyValuePair<string,string>> list = dic.OrderBy(r => r.Key).ToList<KeyValuePair<string, string>>();
       //dic =dic.OrderByDescending(r => r.Key).ToDictionary(r => r.Key, r => r.Value);
foreach (KeyValuePair<string, string> kv in list) { Console.WriteLine(kv.Key + " " + kv.Value); }

 

C# Dictionary 应用

标签:

原文地址:http://www.cnblogs.com/hellonet/p/4332299.html

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