输出的结果为:添加前al的元素个数为:0
添加后al的元素个数为:3
删除C后al的元素个数为:2
三、Hashtable元素的遍历
遍历哈希表需要用到DictionaryEntry(字典键/值对)Object。
例三、利用foreach语句对哈希表进行遍历
- <span style="font-size:18px;">using System; using System.Collections;//需要添加的命名空间
- using System.Collections.Generic; using System.Linq;
- using System.Text; using System.Threading.Tasks;
- namespace 哈希表
- { class Program
- { static void Main(string[] args)
- { Hashtable al = new Hashtable();
- Console.WriteLine("添加前al的元素个数为:"+al.Count); al.Add("1", "a");
- al.Add("2", "b"); al.Add("3", "c");
- Console.WriteLine("添加后al的元素个数为:"+al.Count); foreach (DictionaryEntry t in al)
- { Console.Write("键位:"+t.Key+" 值为:");
- Console.WriteLine(t.Value); }
- Console.ReadLine(); }
- } }</span>
输出的结果为:添加前al的元素个数为:0
添加后al的元素个数为:3
键位:1 值为:a
键位:2 值为:b










