一.基础篇                                  

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. [stm32] NRF24L01+USART搞定有线和无线通信

    前言 一般进行远程监控时,2.4G无线通信是充当远程数据传输的一种方法.这时就需要在现场部分具备无线数据发送装置,而在上位机部分由于一般只有串口,所以将采集到的数据送到电脑里又要在上位机端设计一个数据 ...

  2. linux bash & profile &bash_profile 小结

    login 方式:: su - oracle 依次 /etc/bash.bashrc———— /home/$user/.bashrc ———— /ect/profile ———— /home/$use ...

  3. Node.js与Sails~Model数据模型

    回到目录 对于Sails来说,它的Model与数据库对应,不过它并没有采用目前比较流行的poco贫血模型,而是采用了类似DDD的充血模型,即它的数据实体里即有数据库字段(属性)而且还有方法,而模型里的 ...

  4. EF架构~为EF DbContext生成的实体添加注释(T5模板应用)

    回到目录 相关文章系列 第八回 EF架构~将数据库注释添加导入到模型实体类中 第二十一回  EF架构~为EF DbContext生成的实体添加注释(T4模板应用) 第二十二回EF架构~为EF DbCo ...

  5. Java构造函数

    构造函数的定义: 构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个 ...

  6. [Redis]Redis 概述及基本使用规范.

    1 nosql的简介 1.1 nosql简介 随着互联网Web2.0网站的兴起,传统的关系数据库在应付Web2.0网站,特别是超大规模和高并发的SNS类型的Web2.0纯动态网站已经显得力不从心,暴露 ...

  7. Atitit 软件项目非法模块与功能的管理与  监狱管理的对比 原理与概论attilax总结

    Atitit 软件项目非法模块与功能的管理与  监狱管理的对比 原理与概论attilax总结 软件项目中的非法模块非法功能非法分子与人类中的非法分子很是相似,必须要建议不同的的约束管理标准化... 软 ...

  8. iOS----单例模式(Singleton)

    单例的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例:二是 ...

  9. PHP实现RESTful风格的API实例(三)

    接前一篇PHP实现RESTful风格的API实例(二) .htaccess :重写URL,使URL以 /restful/class/1 形式访问文件 Options +FollowSymlinks R ...

  10. 24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package zhongqiuzuoye; public class Car { String brand; public void drive() {} } package zhongqiuzuo ...