Dictionary的用法及用途
Dictionary<string, string>是一个泛型
他本身有集合的功能有时候可以把它看成数组
他的结构是这样的:Dictionary<[key], [value]>
他的特点是存入对象是需要与[key]值一一对应的存入该泛型
通过某一个一定的[key]去找到对应的值
举个例子:
//实例化对象
Dictionary<int, string> dic = new Dictionary<int, string>();
//对象打点添加
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "one");
//提取元素的方法
string a = dic[1];
string b = dic[2];
string c = dic[3];
//1、2、3是键,分别对应“one”“two”“one”
//上面代码中分别把值赋给了a,b,c
//注意,键相当于找到对应值的唯一标识,所以不能重复
//但是值可以重复
如果你还看不懂我最后给你举一个通俗的例子
有一缸米,你想在在每一粒上都刻上标记,不重复,相当于“键”当你找的时候一一对应不会找错,这就是这个泛型的键的-作用,而米可以一样,我的意思你明白了吧?
-------------------------------------------------------------------------
c# 对dictionary类进行排序用什么接口实现
如果使用.Net Framework 3.5的话,事情就很简单了。呵呵。
如果不是的话,还是自己写排序吧。
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace DictionarySorting
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu");
var result = from pair in dic orderby pair.Key select pair;
foreach (KeyValuePair<int, string> pair in result)
{
Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
}
【执行结果】
Key:1, Value:HaHa
Key:2, Value:HiHi
Key:3, Value:HeHe
Key:4, Value:HuHu
Key:5, Value:HoHo
最新文章
- 【MongoDB初识】-结合C#简单使用,驱动2.x
- php怎么获取mac地址?
- 内存管理_JAVA内存管理
- [CareerCup] 8.9 An In-memory File System 内存文件系统
- redis提示Could not get a resource from the pool(jedis连接池配置)
- PHP设计模式之装饰者模式
- struts 中 s:iterator 使用注意事项
- iOS序列化与反序列化
- uva 10934 Dropping water balloons(转载)
- CachedRowSet的用法
- tooltip 鼠标移动上去出现图片或文字与title大同小异
- GCJ Round 1C 2009 Problem C. Bribe the Prisoners
- 使用Hbuilder手机debug
- list实现大整数加法
- Python手记(二)
- Xamarin.Android 使用SoundPool进行音频播放
- Day16作业及默写
- AForge.NET简介
- OneZero第二周第二次站立会议(2016.3.29)
- Steady Cow Assignment POJ - 3189 (最大流+匹配)