Hashtable表操作

来源:互联网 发布:西部域名 编辑:程序博客网 时间:2024/05/17 22:36
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); } } } 


 

原创粉丝点击