Hashtable 键值对集合

发布时间 2023-03-22 21:10:57作者: 春哥博客


using System; using System.Collections; namespace Hashtable_键值对集合 { class Program { static void Main(string[] args) { //创建一个键值对集合对象 Hashtable ht = new Hashtable(); ht.Add(1,"张三"); ht.Add(2,true); ht.Add(false, "错误的"); //在键值对集合中,是根据键去找值的 Console.WriteLine(ht[1]); //使用foreach循环,集合里的每一项都打印出来 //var:根据值推断数据类型,item:每一项 //遍历集合的键Keys,也可以遍历即可的值Values foreach (var item in ht.Keys) { Console.WriteLine("键是{0},值是{1}",item,ht[item]); } } } }
 
 
分类: C#