C# 集合类(六):Dictionary 泛型集合

http://www.cnblogs.com/wf225/archive/2008/01/14/1038104.html

 

泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。
   
很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:

非泛型集合类

泛型集合类

ArrayList

List<T>

HashTable

DIctionary<T>

Queue

Queue<T>

Stack

Stack<T>

SortedList

SortedList<T>


   
我们用的比较多的非泛型集合类主要有 ArrayList HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据类型相对确定的化  Dictionary<TKey,TValue> 集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用 Dictionary<string, int> 来存储购物车信息,而不需要任何的类型转化。

   
下面是简单的例子,包括声明,填充键值对,移除键值对,遍历键值对

http://www.cnblogs.com/Images/OutliningIndicators/None.gif    Dictionary<stringstring> myDic = new Dictionary<stringstring>();
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    myDic.Add(
"aaa""111");
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    myDic.Add(
"bbb""222");
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    myDic.Add(
"ccc""333");
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    myDic.Add(
"ddd""444");
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//如果添加已经存在的键,add方法会抛出异常
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    try
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        myDic.Add(
"ddd","ddd");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
catch (ArgumentException ex)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"此键已经存在:" + ex.Message);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//解决add()异常的方法是用ContainsKey()方法来判断键是否存在
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    if (!myDic.ContainsKey("ddd"))
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        myDic.Add(
"ddd""ddd");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
else
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"此键已经存在:");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//而使用索引器来负值时,如果建已经存在,就会修改已有的键的键值,而不会抛出异常
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    myDic ["ddd"]="ddd";
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    myDic[
"eee"] = "555";
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//使用索引器来取值时,如果键不存在就会引发异常
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    try
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"不存在的键/"fff/"的键值为:" + myDic["fff"]);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
catch (KeyNotFoundException ex)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"没有找到键引发异常:" + ex.Message);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//解决上面的异常的方法是使用ContarnsKey() 来判断时候存在键,如果经常要取健值得化最好用 TryGetValue方法来获取集合中的对应键值
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    string value = "";
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
if (myDic.TryGetValue("fff"out value))
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"不存在的键/"fff/"的键值为:" + value );
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
else
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{     
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"没有找到对应键的键值"); 
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//下面用foreach 来遍历键值对
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    //
泛型结构体 用来存储健值对
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    foreach (KeyValuePair<stringstring> kvp in myDic)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"key={0},value={1}", kvp.Key, kvp.Value);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//获取值得集合
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    foreach (string s in myDic.Values)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"value={0}", s);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
//获取值得另一种方式
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
    Dictionary<stringstring>.ValueCollection values = myDic.Values;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
foreach (string s in values)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif    
http://www.cnblogs.com/Images/dot.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Console.WriteLine(
"value={0}", s);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif    }


常用的属性和方法如下:

 

 

常用属性

属性说明

 

Comparer

获取用于确定字典中的键是否相等的 IEqualityComparer

 

Count

获取包含在 Dictionary 中的键/值对的数目。

 

Item

获取或设置与指定的键相关联的值。

 

Keys

获取包含 Dictionary 中的键的集合。

 

Values

获取包含 Dictionary 中的值的集合。

 

常用的方法

方法说明

 

Add

将指定的键和值添加到字典中。

 

Clear

Dictionary 中移除所有的键和值。

ContainsKey

确定 Dictionary 是否包含指定的键。

 

ContainsValue

确定 Dictionary 是否包含特定值。

 

Equals 

已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)

 

GetEnumerator

返回循环访问 Dictionary 的枚举数。

 

GetHashCode 

用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)

 

GetObjectData

实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary 实例所需的数据。

 

GetType 

获取当前实例的 Type。 (从 Object 继承。)

 

OnDeserialization

实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。

 

ReferenceEquals 

确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)

 

Remove

Dictionary 中移除所指定的键的值。

 

ToString 

返回表示当前 Object String。 (从 Object 继承。)

 

TryGetValue

获取与指定的键相关联的值。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值