Hashtable 键值对集合

 

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]);
            }
        }
    }
}

 

posted @ 2022-11-20 18:02  小春博客  阅读(20)  评论(0编辑  收藏  举报