110,538
社区成员
发帖
与我相关
我的任务
分享
//Hashtable 表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
Hashtable ht = new Hashtable();
ht.Add(1, null);
ht.Add(2, "2");
ht.Add(3, "3");
ht.Add(4, "4");
Console.WriteLine("\nht.ContainsKey(3) = {0}\tht.ContainsValue(\"3\") = {1}", ht.ContainsKey(3), ht.ContainsValue("3"));
//System.Collections.IEnumerator ien = ht.GetEnumerator();
//ht.Remove(1); //移除键
//ht.Clear(); //清除所有的键
//1
foreach (int item in ht.Keys )
{
Console.WriteLine("key = {0}\t value = {1}", item, ht[item]);
}
//2
foreach (DictionaryEntry item in ht) //DictionaryEntry 定义可设置或检索的字典键/值对。
{
Console.WriteLine("{0} {1}", item.Key, item.Value);
}
Console.WriteLine("-------------------------------------------------");
ICollection valuecoll = ht.Values; //ICollection 定义所有非泛型集合的大小、枚举数和同步方法。
Console.WriteLine("valuecoll.Count = {0}", valuecoll.Count.ToString()); //valuecoll.Count = 4
foreach (string item in valuecoll)
{
Console.WriteLine("value = {0}", item);
}
Console.WriteLine();
ICollection keycoll = ht.Keys;
foreach (int item in keycoll )
{
Console.WriteLine("key = {0}", item);
}
Console.WriteLine("-------------------------------------------------");