HashTable与Dictionary<TKey, TValue>
时间:2011-02-28 来源:墟零
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中的值的集合。
  
   常用方法:
  MyDictionary.Add (string key,double value);
  将指定的键和值添加到字典中。
   
  MyDictionary.Clear ();
  从 Dictionary中移除所有的键和值。 
   
  MyDictionary.ContainsKey(string key);
  确定 Dictionary是否包含指定的键。
   
  MyDictionary.ContainsValue(double value)
   确定 Dictionary是否包含特定值。
   
    Equals 
   已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
   
  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









