碧空雪狐

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Hashtable 和 Dictionary <K, V> 类型 
 1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.
 2:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.
 3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.

 

Hashtable 类和 Dictionary<(Of <(TKey, TValue>)>) 泛型类实现 IDictionary 接口

Dictionary<(Of <(TKey, TValue>)>) 泛型类还实现 IDictionary<(Of <(TKey, TValue>)>) 泛型接口。因此,这些集合中的每个元素都是一个键/值对。

Dictionary<(Of <(TKey, TValue>)>) 类与 Hashtable 类的功能相同
对于值类型,特定类型(不包括 Object)的 Dictionary<(Of <(TKey, TValue>)>) 的性能优于 Hashtable,这是因为 Hashtable 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱操作。

HashTable ht=new HashTable();//实现 IDictionary接口
ht.Add(1,"A");
ht.Add(2,"B");
ht.Add(3,"c");
foreach(DictionaryEntry de in ht)//HashTable返回的是DictionaryEntry类型
 {
    de.Key;
    de.Value;
 }

Dictionary<int,string> myDictionary=new Dictionary<int,string>();//实现IDictionary接口,IDictionary<T key,T value>类
myDictionary.Add(1,"a");
myDictionary.Add(2,"b");
myDictionary.Add(3,"c");
foreach(int i in myDictionary.Keys)
{
  Console.WriteLine("Key="+i+"Value="+myDictionary);
}
Or
foreach(KeyValuePair<string, double> temp in myDictionary)//返回的是KeyValuePair<string, double>泛型数组
  {
       temp.Key;
       temp.Value;
  }

 //使用索引器来取值时,如果键不存在就会引发异常
    try
    {
        Console.WriteLine("不存在的键""fff""的键值为:" + myDic["fff"]);
    }
    catch (KeyNotFoundException ex)
    {
        Console.WriteLine("没有找到键引发异常:" + ex.Message);
    }
    //解决上面的异常的方法是使用ContarnsKey() 来判断时候存在键,如果经常要取健值得化最好用 TryGetValue方法来获取集合中的对应键值
    string value = "";
    if (myDictionary.TryGetValue("fff", out value))
    {
        Console.WriteLine("不存在的键""fff""的键值为:" + value );
    }
    else
    {     
        Console.WriteLine("没有找到对应键的键值"); 
    }
    
    //下面用foreach 来遍历键值对
    //泛型结构体 用来存储健值对
    foreach (KeyValuePair<string, string> temp in myDictionary)
    {
        Console.WriteLine( temp .Key, temp .Value);
    }
    //获取值得集合
    foreach (string s in myDictionary.Values)
    {
        Console.WriteLine("value"+ s);
    }
    //获取值得另一种方式
    Dictionary<string, string>.ValueCollection values = myDictionary.Values;
    foreach (string s in values)
    {
        Console.WriteLine("value:"+ s);
    }
常用的属性和方法如下:  常用属性
 属性说明
 Comparer 
 获取用于确定字典中的键是否相等的 IEqualityComparer。 
 Count 
 获取包含在 Dictionary中的键/值对的数目。
 Item 
 获取或设置与指定的键相关联的值。
 Keys 
 获取包含 Dictionary中的键的集合。
  Values 
 获取包含 Dictionary中的值的集合。
 
 常用的方法 方法说明  
  Add   MyDictionary.Add (string key,double value);
 将指定的键和值添加到字典中。 
 
  Clear    MyDictionary.Clear ();
 从 Dictionary中移除所有的键和值。 
 
ContainsKey    MyDictionary.ContainsKey(string key);
 确定 Dictionary是否包含指定的键。 
 ContainsValue    MyDictionary.ContainsValue(double value)
 确定 Dictionary是否包含特定值。 
 
  Equals  
 已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
 
  GetEnumerator  MyDictionary.GetEnumerator(new Dictionary<string,double >("aaa",11.11);
 返回循环访问 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   MyDictionary.TryGetValue(string key,out double value);注意因为用到了out所有必须在前面声明个变量

即:double valueTest

 if(!MyDictionary.TryGetValue(key,out valueTest))

    valueTest=0.0;   //如果不存在这样的KEY VALUE那么 valueTest=0.0 ,要么就把真实的value赋给valueTest

posted on 2012-02-03 16:40  碧空雪狐  阅读(631)  评论(0编辑  收藏  举报