浅谈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语法规则 ...
随机推荐
- 一个不陌生的JS效果-marquee,用css3来实现
关于marquee,就不多说了,可以戳这里. 毕竟他是一个很古老的元素,现在的标准里头也不推荐使用这个标签了.但平时一些项目中会经常碰到这样的效果,每次都是重新写一遍,麻烦! JS类实现marquee ...
- (翻译)反射处理java泛型
当我们声明了一个泛型的接口或类,或需要一个子类继承至这个泛型类,而我们又希望利用反射获取这些泛型参数信息.这就是本文将要介绍的ReflectionUtil就是为了解决这类问题的辅助工具类,为java. ...
- ACEXML解析XML文件——简单示例程序
掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...
- resumablejs 分块上传 断点续传
http://www.resumablejs.com/ 官网 upload.html <!DOCTYPE html> <html lang="en"> &l ...
- 【重要更新】Senparc.Weixin.Open v1.5.1
本次更新调整了命名空间和文件位置,具体变化为(可以直接在源代码中替换): 旧命名空间(对应文件夹) 新命名空间(对应文件夹) Senparc.Weixin.Open.OAuth Senparc.Wei ...
- IoC组件Unity再续~根据类型字符串动态生产对象
回到目录 这个根据类型字符串动态去生产一个接口的对象,在实现项目中用途很广,这即省去了配置config文件的麻烦,又使用生产对象变更可配置,你完全可以把这种多态持久化到数据库里或者XML文件里,在使用 ...
- 8 步搭建 Node.js + MongoDB 项目的自动化持续集成
任何事情超过 90 秒就应该自动化,这是程序员的终极打开方式.Automating shapes smarter future. 这篇文章中,我们通过创建一个 Node.js + MongoDB 项目 ...
- 代码生成AnimatorController
0.出发点 现在的项目需要设置多套动画组合,全部是由策划在XML文件中设置完成,如果完全的手动在AnimatorController中去做不但工作量大而且如果将来有配置修改了还要一个个去找到对应的自状 ...
- 初次使用IDEA的相关技巧
前言:由于初次使用IDEA,所以很多配置都不是非常熟悉,经过一下午慢慢熟悉和同事的帮助,终于有所斩获,现在我把这个总结写出来,希望能够帮助初次使用的java工程师. 1:下载和安装 下载地址:http ...
- MySQL的用户和权限介绍
一.关于MySQL权限的几点常识: 1.MySQL的权限系统主要用来验证用户的操作权限. 2.在MySQL内部,权限信息存放在MySQL数据库的granttable里.当mysql启动后,grantt ...