C# 泛型字典 Dictionary的使用

在这里插入图片描述

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

namespace L_Dictionary
{
    class Program
    {
        static void printDict(Dictionary<int, string> dict) 
        {
            if(dict.Count == 0)
            {
                Console.WriteLine("--没有数据");
                return;
            }
            else
            {
                Console.WriteLine("--打印数据");
            }

            foreach (KeyValuePair<int, string> item in dict)
            {
                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Dictionary 的基本使用");

            #region  本质
            // 拥有Hashtable的泛型
            // 以键、值对的方式存储   字典(键不能重复)

            #endregion

            #region  申明
            // 需要引用命名空间
            // using System.Collections.Generic;

            Dictionary<int, string> dict = new Dictionary<int, string>();
            #endregion

            #region
            Console.WriteLine("-----------------------增");
            // 键值 不能重复
            dict.Add(1, "123");
            dict.Add(2, "234");
            dict.Add(3, "345");
            //dict.Add(3, "345");  // 报错
            printDict(dict);
            #endregion

            #region  删除
            Console.WriteLine("-----------------------删除");
            // 只能通过键去删除
            // 删除不存在的 键, 没有反应。
            Console.WriteLine("删除键 --- 1");
            dict.Remove(1);
            Console.WriteLine("删除键 --- 4");
            dict.Remove(4);
            printDict(dict);

            // 清空
            Console.WriteLine("清空 ----");
            dict.Clear();
            printDict(dict);

            dict.Add(1, "123");
            dict.Add(2, "234");
            dict.Add(3, "345");
            #endregion

            #region  查询
            Console.WriteLine("-----------------------查询");
            // 1.通过键找到 对应的值 
            //  如果键不存在 报错!
            Console.WriteLine("查询键2: " + dict[2]);
            // Console.WriteLine(dict[4]); // 报错
            Console.WriteLine("查询键1: " + dict[1]);

            // 2.查询是否存在
            //    根据键查询
            if (dict.ContainsKey(1))
            {
                Console.WriteLine("查询 存在键值 1的元素");
            }
            // 根据值检测
            if (dict.ContainsValue("345"))
            {
                Console.WriteLine("查询 存在\"345 \"值的元素");
            }

            #endregion

            #region
            Console.WriteLine("-----------------------改");
            Console.WriteLine("修改 键=1 元素 值= \"666\" ");
            dict[1] = "666";
            printDict(dict);

            #endregion

            #region  遍历
            Console.WriteLine("-----------------------遍历");
            // 1.遍历所有键
            Console.WriteLine("---遍历键");
            foreach (int item in dict.Keys)
            {
                Console.WriteLine(item);
            }

            // 2.遍历所有值
            Console.WriteLine("---遍历所有值");
            foreach (string item in dict.Values)
            {
                Console.WriteLine(item);
            }
            // 3.遍历所有键值
            Console.WriteLine("---遍历所有键值");
            foreach (KeyValuePair<int, string> item in dict)
            {
                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);
            }
            #endregion

            Console.ReadLine();
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值