栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# 泛型字典 Dictionary的使用

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 dict) 
        {
            if(dict.Count == 0)
            {
                Console.WriteLine("--没有数据");
                return;
            }
            else
            {
                Console.WriteLine("--打印数据");
            }

            foreach (KeyValuePair 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 dict = new Dictionary();
            #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 item in dict)
            {
                Console.WriteLine("Key: " + item.Key + "  Value: " + item.Value);
            }
            #endregion

            Console.ReadLine();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923170.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号