一.基础篇                                  

1.Dictionary泛型类提供了从一组键到一组值的映射,即键和值的集合类。

2.Dictionary通过键来检索值的速度是非常快的,这是因为 Dictionary 类是作为一个哈希表来实现的。

3.定义方式:

 Dictionary<[Key], [Value]> openWith = new Dictionary<[Key], [Value]>();

 其中:Key代表此泛型类的键,不可重复。

    Value代表此泛型类中键对应的值。

    Key和Value可以用int,decimal,float等值类型,也可以用string,class等引用类型。

 举例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Test();
} public void Test()
{ //Key为值类型 Value为值类型
Dictionary<int, int> dicInt = new Dictionary<int, int>();
//Key为值类型 Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
//Key为引用类型 Value为引用类型
Dictionary<TestInfo, TestInfo> dicTestClass = new Dictionary<TestInfo, TestInfo>();
}
} public class TestInfo
{
public string ID { get; set; } public string Name { get; set; } public string Pwd { get; set; }
}
}

4.添加键值对的方式:

 ①Dictionary.Add(Key,Value)方式: 

            //Key为值类型   Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString.Add(,"");
dicString.Add(, "");
dicString.Add(, "");
dicString.Add(, "");

 ②Dictionary[Key]=Value 方式:

            //Key为值类型   Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString[] = "";
dicString[] = "";
dicString[] = "";
dicString[] = "";

 ③两种方式对比:

  相同:二者皆可添加键值对。

  差异:第一种方式,当键不存在,相当于插入此键值对,当插入重复键时,则会引发ArgumentException类型的异常。

     第二种方式,当键不存在,相当于插入此键值对,当键存在时,相当于修改该键对应的值;当Dictionary[Key]取值

      时,如果此Key不存在,则会引发KeyNotFoundException异常。

  总结:添加键值对,推荐以第二种为主。

二.进阶篇                                         

1.ContainsKey(Key)判断键中是否含有此Key

            //Key为值类型   Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString[] = "";
dicString[] = "";
dicString[] = "";
dicString[] = ""; if (dicString.ContainsKey())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在该键');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在该键');</script>");
}

2.ContainsValue(Value)判断值中是否含有此Value

            //Key为值类型   Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString[] = "";
dicString[] = "";
dicString[] = "";
dicString[] = ""; if (dicString.ContainsValue(""))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('存在该值');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不存在该值');</script>");
}

3.Foreach和KeyValuePair<Key, Value>配合取出所有键值对

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Key为值类型 Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString[] = "";
dicString[] = "";
dicString[] = "";
dicString[] = ""; foreach (KeyValuePair<int, string> item in dicString)
{
Console.WriteLine("Key={0},Value={1}",item.Key,item.Value);
} Console.ReadKey();
}
}
}

4.Dictionary.Keys,Dictionary.Values和foreach配合取出所有key,所有value。(也可以利用第三步中单独取key和value)

            //取出所有key
foreach (int key in dicString.Keys)
{
Console.WriteLine("Key={0}", key);
}
//取出所有value
foreach (string value in dicString.Values)
{
Console.WriteLine("Value={0}",value);
}

5.Dictionary.Clear()和Dictionary.Remove(Key)移除键值对

            //移除所有的键和值
dicString.Clear();
//移除键为0的键值对
dicString.Remove();

6.Dictionary.Reverse()进行反转序列输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Key为值类型 Value为引用类型
Dictionary<int, string> dicString = new Dictionary<int, string>();
dicString[] = "";
dicString[] = "";
dicString[] = "";
dicString[] = ""; //原始排序输出
foreach (KeyValuePair<int,string> item in dicString)
{
Console.WriteLine("Key={0},Value={1}",item.Key,item.Value);
} //进行反转排序,存入iList
IEnumerable<KeyValuePair<int,string>> iList = dicString.Reverse(); Console.WriteLine("-----------------------------------------");
//反转输出
foreach (KeyValuePair<int, string> item in iList)
{
Console.WriteLine("Key={0},Value={1}", item.Key, item.Value);
} Console.ReadKey(); //输出结果如下: /*
Key=0,Value=00001
Key=1,Value=00002
Key=2,Value=00003
Key=3,Value=00004
------------------------------------------------
Key=3,Value=00004
Key=2,Value=00003
Key=1,Value=00002
Key=0,Value=00001
*/
}
}
}

浅谈Dictionary用法的更多相关文章

  1. 浅谈hover用法

    在前端页面制作中,我们时常要用到移动显示.隐藏的动态效果,我们一般采用js来实现此效果.不过在大部分情况下,我们也可以使用hover来实现此动态效果. 在此,我谈一谈我对hover的用法,请看以下代码 ...

  2. 浅谈 echarts 用法

    对于服务型的公司来说,需要了解用户的使用趋势,来帮助分析市场的走向,所以说统计在一个管理后台中是必不可少的. 会用到echarts插件 ,其官网网址 http://echarts.baidu.com/ ...

  3. 浅谈Python在信息学竞赛中的运用及Python的基本用法

    浅谈Python在信息学竞赛中的运用及Python的基本用法 前言 众所周知,Python是一种非常实用的语言.但是由于其运算时的低效和解释型编译,在信息学竞赛中并不用于完成算法程序.但正如LRJ在& ...

  4. 浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别

    浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别 Spring 2.5 版本新增了注解功能, 通过注解,代码编写简化了很多:但熟悉注解的使 ...

  5. 浅谈HTTP中GET、POST用法以及它们的区别

    浅谈HTTP中GET.POST用法以及它们的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符.我们可以这样认为: 一 ...

  6. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  7. [技术]浅谈OI中矩阵快速幂的用法

    前言 矩阵是高等代数学中的常见工具,也常见于统计分析等应用数学学科中,矩阵的运算是数值分析领域的重要问题. 基本介绍 (该部分为入门向,非入门选手可以跳过) 由 m行n列元素排列成的矩形阵列.矩阵里的 ...

  8. [C#]6.0新特性浅谈

    原文:[C#]6.0新特性浅谈 C#6.0出来也有很长一段时间了,虽然新的特性和语法趋于稳定,但是对于大多数程序猿来说,想在工作中用上C#6.0估计还得等上不短的一段时间.所以现在再来聊一聊新版本带来 ...

  9. 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变

    在net中json序列化与反序列化   准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...

随机推荐

  1. 【Bugly干货分享】微信文件微起底Ⅰ

    Bugly 技术干货系列内容主要涉及移动开发方向,是由Bugly邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处 微信大家都在用,但微信的本地文件到底隐藏 ...

  2. hadoop 笔记(hbase)

    hbase 基础: hbase是基于列的数据,其数据模式如下: 1.安装 1.1)hbase安装分为单机.伪分布式.分布式,单机下安装不依赖于hadoop:因为不需要分布式文件系统支持: 1.2)安装 ...

  3. PostgreSQL基础整理(二)

    存储过程 实现功能:针对工资表30岁以下,工资提升10% 30至40提升20% 40以上提升30% + 奖金(入参)返回平均薪酬 创建表: DROP TABLE emps; CREATE TABLE ...

  4. 关于Xcode5的离线帮助

    关于Xcode的离线帮助文档,网上找到的许多都是Xcode4的资料,Xcode5貌似将文档搬到了Help菜单里,而不是原先的<Window> - <Organizer> - & ...

  5. Unity3D shader简介

    Unity3D shader简介 可以肯定的说Unity3D使得很多开发者开发游戏更容易.毫无疑问,shader(着色器)编码,仍有很长的路要走.shader是一个专门运行在GPU的程序,经常被神秘包 ...

  6. 用于主题检测的临时日志(431b1c14-8b75-4f42-994f-cfda72208c10 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(3bf68152-fcac-4628-92d6-3f8f4d5e0ee4 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  7. 用python实现的百度新歌榜、热歌榜下载器

    首先声明,本工具仅仅为学习之用,不涉及版权问题,因为百度音乐里面的歌曲本身是可以下载的,而且现在百度也提供了”百度音乐播放器”,可以通过这个工具进行批量下载. 我当时做这个工具的时候,百度还没有提供” ...

  8. .net版本发展历史

    最近装上了VS2013,发现好多新特性.新功能,公司办公还在使用VS2005.VS2008,不过用着也很顺手,在最新版Visual Studio中,微软加入了git源码管理工具,和之前的TFS大体上类 ...

  9. jQuery的extend方法的深层拷贝

    一些东西长时间不用就忘了,比如这个jQuery的extend方法的深层拷贝,今天看单页应用的书的时候,看到entend第一个参数是true,都蒙了.也是,自己的大部分对jQuery的学习知识来自锋利的 ...

  10. HTML学习入门

    HTML(元素.属性) HTML: 超文本标记语言   1. 超文本即为带有链接属性的文本  2.标记即为标签 一.body属性: bgcolor:页面背景颜色 text:文字颜色 backgroun ...