浅谈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语法规则 ...
随机推荐
- [ACM_动态规划] 最长上升子序列(LIS)
问题描述:给n个数,找出最长子序列并输出 问题分析:本题是DAG(有向无环图)最长路问题,设d[i]为以i结尾的最长链的长度,则状态转移方程为:d[i]=max{0,d[j]|j<i & ...
- 用curl向指定地址POST一个JSON格式的数据
昨天的一个任务,用POST 方式向一个指定的URL推送数据.以前都用的数组来完成这个工作. 现在要求用json格式.感觉应该是一样的.开写. <?php $post_url = "ht ...
- Ember.js实现单页面应用程序
1.1.1 摘要 单页应用程序 (SPA) 是加载单个HTML 页面并在用户与应用程序交互时动态更新该页面的Web应用程序. SPA使用AJAX和HTML5创建流畅且响应迅速的Web应用程序,不会经常 ...
- windows进程通信 -- WM_COPYDATA消息
WM_COPYDATA消息,在win32中用来进行进程间的数据传输. typedef struct tagCOPYDATASTRUCT { // cds DWORD dwData; DWORD cbD ...
- 小计C/C++问题(1)
本文主要记录了以下2个问题: 表达式中,有符号变量和无符号变量的转化问题 C/C++中,main函数执行完以后,还执行了什么语句? 这里简单的说一下我的环境:Win7 32位,Qt creator 5 ...
- Redis主从复制问题和扩容问题的解决思路
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/106.html?1455867541 一.解决主从复制问题 当使用Redi ...
- PSR规范
背景 Framework Interoperability Group(框架可互用性小组),简称 FIG,成立于 2009 年.FIG 最初由几位知名 PHP 框架开发者发起,在吸纳了许多优秀的大脑和 ...
- Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范
Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范 需要增加的jsr规范1 开发常用的10个规范(jsp etc)1 other开发常用的50个规范1 需要 ...
- Atititi tesseract使用总结
Atititi tesseract使用总结 消除bug,优化,重新发布.当前版本为3.02 项目下载地址为:http://code.google.com/p/tesseract-ocr. Window ...
- JS的prototype和__proto__ Constructor
一.prototype和__proto__的概念 prototype是 注意是 只有函数的一个属性才有的(每个函数都有一个prototype属性),这个属性是一个指针,指向一个普通对象并且不是原型对象 ...