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 应用的更多相关文章

  1. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  2. ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary

    AR.Global 文档 1:对象或属性: 名称 类型 说明 DG 对象 DataGrid操作对象 //datagrid集合,根据ID取出DataGrid对象,将Json当数组用. Items: ne ...

  3. WebAPI接口返回ArrayList包含Dictionary对象正确解析

    一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...

  4. Linq在Array,List,Dictionary中的应用

    Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...

  5. python之最强王者(8)——字典(dictionary)

    1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...

  6. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  7. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  8. Dictionary

    命名空间:System.Collections.Generic(程序集:mscorlib) Dictionary<TKey, TValue> 类   一般用法:通过key获取value,k ...

  9. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

  10. Dictionary Learning(字典学习、稀疏表示以及其他)

    第一部分 字典学习以及稀疏表示的概要 字典学习(Dictionary Learning)和稀疏表示(Sparse Representation)在学术界的正式称谓应该是稀疏字典学习(Sparse Di ...

随机推荐

  1. oracle 空置排在最后显示

    nulls last select * from emp order by comm select * from emp order by comm desc select * from emp or ...

  2. symfony框架在中国移动cmwap网络下访问的问题

    最近用symfony框架给手机app做后台,发现在中国移动cmwap网络下会出现问题,所有请求都路由到根路径了 左图为原始$_SERVER对象,右图为$request->server对象由于re ...

  3. memcache分布式小实例

    <?php /** * 分布式的 memcache set 实现 */ /** * 创建缓存实现memcache 添加分布式服务器 并设置权限 */ function createCache() ...

  4. ANDROID 自动生成动态表格for

    简单的栗子去了解这个自动生成的动态的控件(自动生成表格) /cs-Layout/res/layout/activity_main.xml <LinearLayout xmlns:android= ...

  5. ios app 开发中ipa重新签名步骤介绍-备

    作为一个app应用程序开发者,在app应用程序在苹果商店上架前总需要将安装包安装到ios机器上进行测试,这个时候我们就需要打包in house版本的ipa了,打包in house实际上是一个将ipa应 ...

  6. MFC中DoDataExchange()的作用

    void CDlgSelectCS::DoDataExchange(CDataExchange* pDX) {     CDialog::DoDataExchange(pDX);     DDX_Te ...

  7. 【HDOJ】3316 Mine sweeping

    简单BFS. #include <iostream> #include <cstdio> #include <cstring> #include <cstdl ...

  8. UML建模之状态图(Statechart Diagram)

     状态图目录: 一.状态图简介(Brief introduction) 二.状态图元素(State Diagram Elements) 1.状态(States) 2.转移(Transitions) 3 ...

  9. JavaScript……

    退役了好伤心…… 这几天搞研究性学习写网页版贪吃蛇代码……太蛋疼了 要学javascript,就还要搞AJAX.JQuery.JSON…… 我感觉整个人都不好了

  10. HDU 5505 - BestCoder Round #60 - GT and numbers

    题目链接 : http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=641&pid=1002 思路 : N有若 ...