(四十八)C#编程基础复习——C#哈希表( Hashtable)

在C#中,哈希表类表示根据键的哈希代码进行组织的键(key)/值(value)对的集合,可以使用键来访问集合中的元素。也就是说当您需要使用键来访问制定元素时,可以选择使用哈希表。

一、哈希表类中的属性

下表中列出了哈希表类中一些常用的属性:

二、哈希表类中的方法

下表中列出了哈希表类中一些常用的方法:

示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace _009
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();

            ht.Add("001", "小王");
            ht.Add("002", "小黄");
            ht.Add("003", "小李");
            if(ht.ContainsValue("张三"))
            {
                Console.WriteLine("该学生姓名已存在");
            }
            else
            {
                ht.Add("004", "李四");
            }
            //获取键的集合
            ICollection key = ht.Keys;
            foreach(string k in key)
            {
                Console.WriteLine(k + ":" + ht[k]);
            }
            Console.ReadKey();
        }
    }
}

 

posted @ 2024-01-05 10:59  代号六零一  阅读(102)  评论(0)    收藏  举报