详解C#对Dictionary内容的通用操作

2022-07-14,,,,

一、实现效果

1.1实现的功能

①添加信息到字典中;

②根据键获取值;

③根据值获取键;

④修改指定键的值;

⑤修改指定值为相同信息;

⑥根据键移除信息;

⑦根据值移除信息;

1.2实现的功能效果图

二、实现核心

/***
*	title:"容器" 项目
*		主题:dictionary的帮助类
*	description:
*		功能:
*		    ①添加信息到字典中
*		    ②根据键获取值
*		    ③根据值获取键
*		    ④修改指定键的值
*		    ⑤修改指定值为相同信息
*		    ⑥根据键移除信息
*		    ⑦根据值移除信息
*	version:0.1版本
*	author:coffee
*	modify recoder:
*/
 
using system;
using system.collections.generic;
using system.linq;
using system.text;
 
namespace utils
{
    public class dictionaryhelper
    {
        /// <summary>
        /// 添加信息到字典中
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="key">需添加的键</param>
        /// <param name="value">需添加的值</param>
        public static void addinfotodic<tkey, tvalue>(dictionary<tkey, tvalue> dic, tkey key, tvalue value)
        {
            if (dic == null)
            {
                dic = new dictionary<tkey, tvalue>();
            }
 
            if (dic.containskey(key))
            {
                dic[key] = value;
            }
            else
            {
                dic.add(key, value);
            }
        }
 
        /// <summary>
        /// 根据键获取值
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="key">键</param>
        /// <returns>返回键对应的值</returns>
        public static tvalue getvalueofkey<tkey, tvalue>(dictionary<tkey, tvalue> dic, tkey key)
        {
            tvalue tmpvalue = default(tvalue);
            if (dic != null && dic.count > 0)
            {
                if (dic.containskey(key))
                {
                    tmpvalue = dic[key];
                }
            }
            return tmpvalue;
        }
 
 
        /// <summary>
        /// 根据值获取键
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="value">值</param>
        /// <returns>返回值对应的所有键</returns>
        public static list<tkey> getkeyofvalue<tkey, tvalue>(dictionary<tkey, tvalue> dic, tvalue value)
        {
            list<tkey> keylist = new list<tkey>();
            foreach (keyvaluepair<tkey, tvalue> kv in dic)
            {
                if (kv.value.equals(value))
                {
                    tkey tmpkey = kv.key;
                    keylist.add(tmpkey);
                }
            }
            return keylist;
        }
 
        /// <summary>
        /// 修改指定键的值
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needmodifykey">需要修改的键</param>
        /// <param name="replacevalue">需要替换的值</param>
        /// <returns>返回修改结果(true:表示成功)</returns>
        public static bool modifyinfoofkey<tkey, tvalue>(dictionary<tkey, tvalue> dic, tkey needmodifykey, tvalue replacevalue)
        {
            if (dic == null || dic.count < 1) return false;
 
            if (dic.containskey(needmodifykey))
            {
                dic[needmodifykey] = replacevalue;
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 修改指定值为相同信息
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needmodifyvalue">需要修改的值</param>
        /// <param name="replacevalue">需要替换的值</param>
        public static void modifyinfoofvalue<tkey, tvalue>(dictionary<tkey, tvalue> dic, tvalue needmodifyvalue, tvalue replacevalue)
        {
            if (dic == null || dic.count < 1) return;
 
            for (int i = 0; i < dic.count;)
            {
                tvalue tmpvalue = dic.elementat(i).value;
                if (tmpvalue.equals(needmodifyvalue))
                {
                    tkey tmpkey = dic.elementat(i).key;
                    dic[tmpkey] = replacevalue;
 
                    i = 0;
                }
                else
                {
                    i++;
                }
            }
 
 
        }
 
        /// <summary>
        /// 根据键移除信息
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needdeletekey">需要删除的键</param>
        /// <returns>返回移除结果(true:表示成功)</returns>
        public static bool removeinfoofkey<tkey, tvalue>(dictionary<tkey, tvalue> dic,tkey needdeletekey)
        {
            if (dic.containskey(needdeletekey))
            {
                dic.remove(needdeletekey);
 
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 根据值移除信息
        /// </summary>
        /// <typeparam name="tkey">键类型</typeparam>
        /// <typeparam name="tvalue">值类型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needdeletevalue">需要删除的值</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public static bool removeinfoofvalue<tkey, tvalue>(dictionary<tkey, tvalue> dic, tvalue needdeletevalue)
        {
            if (dic == null || dic.count < 1) return false;
 
            int initcount = dic.count;
 
            for (int i = 0; i < dic.count;)
            {
                tvalue tmpvalue = dic.elementat(i).value;
                if (tmpvalue.equals(needdeletevalue))
                {
                    tkey tmpkey = dic.elementat(i).key;
                    dic.remove(tmpkey);
 
                    i = 0;
                }
                else
                {
                    i++;
                }
            }
 
            if (initcount > dic.count)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
 
    }//class_end
 
}

三、使用方法

3.1引用命名空间

using utils;

3.2使用示例

using system;
using system.collections.generic;
using utils;
 
namespace test_dictionary
{
    class program
    {
        static void main(string[] args)
        {
            console.writeline("hello world!");
 
            //获取到字典信息
            dictionary<string, string> dic = getdictionary();
            console.writeline($"1-开始获取到的字典的所有信息如下:");
            showinfoofdic(dic);
 
            //根据键获取到对应的值
            string querykey = "l1";
            console.writeline($"当前查询的键是:{querykey}");
            string tmpvalue = dictionaryhelper.getvalueofkey(dic,querykey);
            console.writeline($"2-获取到——键:l1对应的值是:{tmpvalue}");
 
            //根据值获取到对应的所有键
            string queryvalue = "23.4";
            console.writeline($"当前查询的值是:{queryvalue}");
            list<string> tmpkey = dictionaryhelper.getkeyofvalue(dic, queryvalue);
            showinfooflist(tmpkey);
 
            //修改指定键的值
            string needmodifykey = "l4";
            string replacevalue1 = "66";
            console.writeline($"当前需要修改的键是:{needmodifykey}_替换为的值是:{replacevalue1}");
            dictionaryhelper.modifyinfoofkey(dic, needmodifykey, replacevalue1);
            console.writeline($"修改的键是:{needmodifykey}_替换为的值是:{replacevalue1}后所有内容如下:");
            showinfoofdic(dic);
 
            //修改指定值为相同信息
            string needmodifyvalue = "23.6";
            string replacevalue = "33.9";
            console.writeline($"当前需要修改的值是:{needmodifyvalue}_替换为的值是:{replacevalue}");
            dictionaryhelper.modifyinfoofvalue(dic,needmodifyvalue,replacevalue);
            console.writeline($"修改的值是:{needmodifyvalue}_替换为的值是:{replacevalue}后所有内容如下:");
            showinfoofdic(dic);
 
            //根据键移除信息
            string curremovekey = "l3";
            console.writeline($"当前移除的键是:{curremovekey}");
            dictionaryhelper.removeinfoofkey(dic,curremovekey);
            console.writeline($"移除的键是:{curremovekey}后所有内容如下:");
            showinfoofdic(dic);
 
            //根据值移除信息
            string curremovevalue = "23.4";
            console.writeline($"当前移除的值是:{curremovevalue}");
            dictionaryhelper.removeinfoofvalue(dic, curremovevalue);
            console.writeline($"移除的值是:{curremovevalue}后所有内容如下:");
            showinfoofdic(dic);
 
            console.readline();
        }
 
        //获取一个字典
        public static dictionary<string, string> getdictionary()
        {
            dictionary<string, string> dic = new dictionary<string, string>();
 
            dictionaryhelper.addinfotodic(dic, "l1","23.4");
            dictionaryhelper.addinfotodic(dic, "l2", "23.6");
            dictionaryhelper.addinfotodic(dic, "l3", "23.8");
            dictionaryhelper.addinfotodic(dic, "l4", "23.4");
            dictionaryhelper.addinfotodic(dic, "l5", "23.6");
            dictionaryhelper.addinfotodic(dic, "l6", "23.4");
 
            return dic;
        }
 
        //显示字典中的所有信息
        private static void showinfoofdic(dictionary<string,string> dic)
        {
            if (dic == null || dic.count < 1) return;
 
            foreach (var item in dic)
            {
                console.writeline($"键:{item.key} 值:{item.value}");
            }
            console.writeline($"--------------显示信息完成______当前字典:{dic.gettype().name} 共有数据:{dic.count} 条\r\n");
        }
 
        //显示列表信息
        private static void showinfooflist(list<string> list)
        {
            if (list == null || list.count < 1) return;
            foreach (var item in list)
            {
                console.writeline($"对应内容:{item}");
            }
            console.writeline($"--------------显示信息完成______当前列表:{list.gettype().name} 共有数据:{list.count} 条\r\n");
        }
 
    }//class_end
}

到此这篇关于详解c#对dictionary内容的通用操作的文章就介绍到这了,更多相关c# dictionary内容操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《详解C#对Dictionary内容的通用操作.doc》

下载本文的Word格式文档,以方便收藏与打印。