HashTable 和Dictionary的區別

來源:互聯網
上載者:User

標籤:blog   http   for   io   cti   div   

黑馬程式員--鄢振東---------------------- ASP.Net+Unity3D遊戲開發、.Net培訓、期待與您交流! ----------------------首先hashtable和Dictionary這2個都是屬於字典類,就是索引值對集合。 相同點:我們都可以對其進行添加索引值對  用add() 函數。和addrange()函數。    還有移除move moverange moveat        還有通過 鍵 去找  值。不同點:對於實值型別,特定類型(不包括 Object)的 Dictionary的效能優於 Hashtable,這是因為 Hashtable 的元素屬於 Object 類型,所以在儲存或檢索實值型別時通常發生裝箱和unboxing操作。

Dictionary類與 Hashtable 類的功能相同

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;
  }

2者都是通過迴圈來便利  來通過鍵得到值的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.