using System;
using System.Collections;
namespace HashtableTest
{
class Program
{
static void Main(string[] args)
{
// 哈希表默认容量是 7,内部数组初始大小为 11
// 每次扩容性能损失相当大,所以最好直接给定 capacity 值
Hashtable htt = new Hashtable(100);
for (int i = 0; i < 100; i++)
htt.Add(i, i);
Console.WriteLine(htt.Count);
// 使用一个副本,退出 foreach 就回收了,不会造成大的性能影响
foreach (int key in ((Hashtable)htt.Clone()).Keys)
if (key % 2 == 0)
htt.Remove(key);
Console.WriteLine(htt.Count);
}
}
}