//引入命名空间 using System.Collections //Hash对象 Hashtable hash=new Hashtable(); //hash由键和值组成 //HashTest.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Hashtable hash = new Hashtable(); //添加数据 hash.Add(1, "lin"); hash.Add(2, "bai"); hash.Add(3, "chuan"); //访问数据,采用键的方式访问 Console.WriteLine(hash[1]); //采用遍历键集合访问数据 //var是推断类型,可根据所获得的数据类型进行匹配对应 var skeys = hash.Keys; //获取hash键的集合 foreach (object o in skeys) { Console.WriteLine("键:{0},值:{1}", o, hash[o]); //从下往上的方式输出 } //采用遍历器 var ie = hash.GetEnumerator();//获取一个遍历器 while (ie.MoveNext()) //从当前行开始读起,依次遍历集合的数据,类似游标 { Console.WriteLine("键:{0},值:{1}", ie.Key, ie.Value); } } } }