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

一、实现效果

1.1实现的功能

①添加信息到字典中;

②根据键获取值;

③根据值获取键;

④修改指定键的值;

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

⑥根据键移除信息;

⑦根据值移除信息;

1.2实现的功能效果图

详解C#对Dictionary内容的通用操作_第1张图片

二、实现核心

/***
*	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
    {
        /// 
        /// 添加信息到字典中
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 需添加的键
        /// 需添加的值
        public static void AddInfoToDic(Dictionary dic, TKey key, TValue value)
        {
            if (dic == null)
            {
                dic = new Dictionary();
            }
 
            if (dic.ContainsKey(key))
            {
                dic[key] = value;
            }
            else
            {
                dic.Add(key, value);
            }
        }
 
        /// 
        /// 根据键获取值
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 键
        /// 返回键对应的值
        public static TValue GetValueOfKey(Dictionary dic, TKey key)
        {
            TValue tmpValue = default(TValue);
            if (dic != null && dic.Count > 0)
            {
                if (dic.ContainsKey(key))
                {
                    tmpValue = dic[key];
                }
            }
            return tmpValue;
        }
 
 
        /// 
        /// 根据值获取键
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 值
        /// 返回值对应的所有键
        public static List GetKeyOfValue(Dictionary dic, TValue value)
        {
            List keyList = new List();
            foreach (KeyValuePair kv in dic)
            {
                if (kv.Value.Equals(value))
                {
                    TKey tmpKey = kv.Key;
                    keyList.Add(tmpKey);
                }
            }
            return keyList;
        }
 
        /// 
        /// 修改指定键的值
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 需要修改的键
        /// 需要替换的值
        /// 返回修改结果(true:表示成功)
        public static bool ModifyInfoOfKey(Dictionary 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;
            }
        }
 
        /// 
        /// 修改指定值为相同信息
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 需要修改的值
        /// 需要替换的值
        public static void ModifyInfoOfValue(Dictionary 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++;
                }
            }
 
 
        }
 
        /// 
        /// 根据键移除信息
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 需要删除的键
        /// 返回移除结果(true:表示成功)
        public static bool RemoveInfoOfKey(Dictionary dic,TKey needDeleteKey)
        {
            if (dic.ContainsKey(needDeleteKey))
            {
                dic.Remove(needDeleteKey);
 
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// 
        /// 根据值移除信息
        /// 
        /// 键类型
        /// 值类型
        /// 字典
        /// 需要删除的值
        /// 返回结果(true:表示成功)
        public static bool RemoveInfoOfValue(Dictionary 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 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 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 GetDictionary()
        {
            Dictionary dic = new Dictionary();
 
            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 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 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内容的通用操作)