浅谈Dictionary用法
一.基础篇
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用法的更多相关文章
- 浅谈hover用法
在前端页面制作中,我们时常要用到移动显示.隐藏的动态效果,我们一般采用js来实现此效果.不过在大部分情况下,我们也可以使用hover来实现此动态效果. 在此,我谈一谈我对hover的用法,请看以下代码 ...
- 浅谈 echarts 用法
对于服务型的公司来说,需要了解用户的使用趋势,来帮助分析市场的走向,所以说统计在一个管理后台中是必不可少的. 会用到echarts插件 ,其官网网址 http://echarts.baidu.com/ ...
- 浅谈Python在信息学竞赛中的运用及Python的基本用法
浅谈Python在信息学竞赛中的运用及Python的基本用法 前言 众所周知,Python是一种非常实用的语言.但是由于其运算时的低效和解释型编译,在信息学竞赛中并不用于完成算法程序.但正如LRJ在& ...
- 浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别
浅谈@RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别 Spring 2.5 版本新增了注解功能, 通过注解,代码编写简化了很多:但熟悉注解的使 ...
- 浅谈HTTP中GET、POST用法以及它们的区别
浅谈HTTP中GET.POST用法以及它们的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符.我们可以这样认为: 一 ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
- [技术]浅谈OI中矩阵快速幂的用法
前言 矩阵是高等代数学中的常见工具,也常见于统计分析等应用数学学科中,矩阵的运算是数值分析领域的重要问题. 基本介绍 (该部分为入门向,非入门选手可以跳过) 由 m行n列元素排列成的矩形阵列.矩阵里的 ...
- [C#]6.0新特性浅谈
原文:[C#]6.0新特性浅谈 C#6.0出来也有很长一段时间了,虽然新的特性和语法趋于稳定,但是对于大多数程序猿来说,想在工作中用上C#6.0估计还得等上不短的一段时间.所以现在再来聊一聊新版本带来 ...
- 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变
在net中json序列化与反序列化 准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...
随机推荐
- 分享google的技能的11个级别,大家看看自己到哪个级别了?
you are unfamiliar with the subject area. you can read / understand the most fundamental aspects of ...
- Javascrpt无刷新文件上传
最近工作中遇到上传文件问题,主要需求是一步点击上传,兼容ie8+,当时用的dojox/form/uploader控件,这两天扒了一下源码,明白了原理拿出来分享一下. 总体思路如下: 1.对于支持XML ...
- 浅谈Excel开发:二 Excel 菜单系统
在开始Excel开发之前,需要把架子搭起来.最直接的那就是Excel里面的菜单了,他向用户直观的展现了我们的插件具有哪些功能.菜单出来之后我们就可以实现里面的事件和功能了.Excel菜单有两种形式,一 ...
- [stm32] SIM808模块之发短信\GPS\TCP\HTTP研究
SIM8008是四频模块,全球可用.含有TTL电平接口等接口,能够实现发短信.打电话.GPRS传输数据.GPS等功能.[正版资料请找beautifulzzzz·博客园] 一些细节: >> ...
- AWS系列之三 使用EBS
Amazon Elastic Block Store(EBS)可作为EC2实例的持久性数据块级存储.其具有高可用性和持久性的特点,可用性高达99.999%.给现有的EC2实例扩展新的存储块只需要几分钟 ...
- 使用finfo_file()函数检测上传图片的类型
该函数可以检测文件的MIME类型.因为有时候我们只根据文件后缀来判断是不准确的. function getMIME($filename){ $finfo = finfo_open(FILEINFO_M ...
- Duplicate id @+id/imageView, already defined earlier in this layout,android
原文地址http://www.thinksaas.cn/topics/0/448/448554.html 其實這個訊息也是可以解掉的,當然最簡單的解法就是你不要使用相同的id就好了.不過萬一你是幫別人 ...
- MyBatis学习总结(七)——Mybatis缓存
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- Atitit usrQBK1600 技术文档的规范标准化解决方案
Atitit usrQBK1600 技术文档的规范标准化解决方案 1.1. Keyword关键词..展关键词,横向拓展比较,纵向抽象细化拓展知识点1 1.2. 标题必须有高大上词汇,参考文章排行榜,1 ...
- jquery 的队列queue
使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...