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);
}
记得System.Collections.Specialized.ListDictionary是一个单向链表的结构,所以如果Key/Value对不多的话,它的效率该比Hashtable高。
System.Collections.Specialized.StringDictionary的话Key是区分大小写的string数据类型。
for(int i = 0 ; i < id.Length ; i ++)
{
if(id[i].Trim() != "")
{
string builId = id[i];
string buildept = G.Getxxxx(builId).Rows[0]["xxxx"].ToString().Trim();
if(!hash.Contains(buildept))
{
hash.Add(buildept,builId);
}
else
{
hash[buildept] = (string)hash[buildept] + "," + builId;
}
}
}
foreach(DictionaryEntry de in hash)
{
string dept = de.Key.ToString().Trim();
string ids = de.Value.ToString().Trim();
}