11_Hashtable集合

Posted on 2020-06-01 20:46  NCY_1991  阅读(351)  评论(0编辑  收藏  举报
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //Hashtable 键值对集合
 6             //键值对集合中,键是唯一的,值可以重复。
 7             Hashtable ht = new Hashtable();
 8             ht.Add(1, "张三");
 9             ht.Add(2, true);
10             ht.Add(3, '');
11             ht.Add(4, 12323);
12             ht.Add(false, "错误的");
13 
14             //ht.Add(1, 123);//!!无法通过该方式修改键对应的值
15             ht[1] = 123;//可以通过该方式修改键对应的值
16 
17             //添加值前先做一个判断
18             if (!ht.Contains(999))
19             {
20                 ht.Add(999, 666);
21             }
22 
23             //在键值对集合中是根据键去找值的
24             foreach (var item in ht.Keys)
25             {
26                 Console.WriteLine("{0}--{1}",item,ht[item]);
27             }
28             
29         }
30     }