以前分析数据的时候,用过这个东西,当时连叫了几声“爽”,今天刚好有又朋友问起这个,补充几点吧。
System.Collections.Hashtable是用来表示一组组key/value结构的容器,可能别的语言会把它叫做Map,Dictionary的,其结构中Key用来快速查找,所以,叫它Dictionary可能更加合适。
Hashtable的方法、属性并不多,大多数也可以望文生正义,只是有一点,Hashtable提供了hashTable[aKey]的方式引用其包含的对象,却并没有提供数字指向的索引器,就是说用惯了Array,数组的我们,不能用hashTable[0]之类的办法来检索它的内容,继而,我们也就不能用
for(int i =0 ; i < hashTable.count ; i ++)
{
Console.WriteLine("Key -- {0}; Value --{1}.",hashTable[i].key,hashTable[i].value);
}
这样的代码块来遍历,而我们把一个个的键值对加入HashTable中,很多情况下都是最终要把它遍历出来,那么怎么做呢?以下代码将解答这个问题。
foreach( DictionaryEntry de in hashTable)
{
console.WriteLine("Key -- {0}; Value --{1}.", de.Key, de.Value);
}
不过还是习惯用index来访问的话,System.Collections.Specialized.NameValueCollection是一个可以用index访问的类,不爽的是它的Key/Value都必须是string数据类型。
记得System.Collections.Specialized.ListDictionary是一个单向链表的结构,所以如果Key/Value对不多的话,它的效率该比Hashtable高。
System.Collections.Specialized.StringDictionary的话Key是区分大小写的string数据类型。
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)