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

C# 字典 Dictionary 的用法

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

C# 字典 Dictionary 的用法

前言
  • C# 中的 字典 Dictionary的用法,可以查看微软MSDN的在线文档
方法验证
  • 这里建一个 C# WinForm的应用工程

字典
  • 字典 Dictionary 就是【键值对】的管理,可以添加多个【键值对】,用处还是比较的广泛的
  • 如学号与姓名的键值对的管理
  • 键(Key)必须是唯一的,值(Value)可以相同可以不相同
  • 为了表达方便,尽量不使用空值(NULL)最为Key 或者 Value
测试代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dictionary_demo
{
    public partial class Form1 : Form
    {
        Dictionary dictDemo = new Dictionary();
        public Form1()
        {
            InitializeComponent();
        }

        private void add_dict_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.AppendText("------------- Add item -------------rn");
            if (dictDemo.ContainsKey(textKey.Text))
            {
                this.textBoxInfo.AppendText("Key : " + textKey.Text + " already exits!rn");
            }
            else
            {
                dictDemo.Add(textKey.Text, textValue.Text);
                this.textBoxInfo.AppendText("Add Key : " + textKey.Text + " rn");
                this.textBoxInfo.AppendText("Add Value : " + textValue.Text + " rn");
            }
        }

        private void btnLogClean_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.Clear();
        }

        private void btnFindFromKey_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.AppendText("------------- find item with Key -------------rn");
            textValue.Text = "";
            if (dictDemo.ContainsKey(textKey.Text))
            {
                string value = "";
                this.textBoxInfo.AppendText("Key : " + textKey.Text + " exits!rn");
                if (dictDemo.TryGetValue(textKey.Text, out value))
                {
                    textValue.Text = value;
                    this.textBoxInfo.AppendText("Find : " + "Key = " + textKey.Text + "Value = " + value + "rn");
                }
                else
                {
                    this.textBoxInfo.AppendText("Key : " + textKey.Text + " find error!rn");
                }
            }
            else
            {
                this.textBoxInfo.AppendText("Key : " + textKey.Text + " not exits!rn");
            }
        }

        private void btnFindFromValue_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.AppendText("------------- find item with value -------------rn");
            foreach (KeyValuePair kvp in dictDemo)
            {
                if (kvp.Value.Equals(textValue.Text))
                {
                    this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " rn");
                }
            }
            this.textBoxInfo.AppendText("------------- print end   -------------rn");
        }

        private void btnDictPrint_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.AppendText("------------- print start -------------rn");
            foreach (KeyValuePair kvp in dictDemo)
            {
                this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " rn");
            }
            this.textBoxInfo.AppendText("------------- print end   -------------rn");
        }

        private void btnDictDelete_Click(object sender, EventArgs e)
        {
            this.textBoxInfo.AppendText("------------- remove item -------------rn");
            dictDemo.Remove(textKey.Text);
            this.textBoxInfo.AppendText("Remove itme : Key = " + textKey.Text + " rn");
        }
    }
}
测试方法

相关的API 初始化
  • Dictionary dictDemo = new Dictionary();
  • 注意 Key 与 Value的类型,可以随便,不一定是string类型
添加
  • 使用 Add 方法添加,如:
    dictDemo.Add(textKey.Text, textValue.Text);
删除
  • 使用 remove 方法,如
    dictDemo.Remove(textKey.Text);
  • 清空字典可以使用 clear方法
  • dictDemo.clear();
查找
  • 按关键字查找,最好先判断关键字是否存在,如:
    if (dictDemo.ContainsKey(textKey.Text))
  • 如果key 存在,可以使用:TryGetValue 尝试获取值,或者直接通过索引:
    string value = dictDemo[textKey.Text];
遍历
  • 使用 KeyValuePair 方法
            foreach (KeyValuePair kvp in dictDemo)
            {
                if (kvp.Value.Equals(textValue.Text))
                {
                    this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " rn");
                }
            }
小结
  • C# 的字典用的还是比较的简单,更多的操作可以详细查看微软官方的文档
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023578.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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