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

目录

一、实现效果

1.1实现的功能

1.2实现的功能效果图

二、实现核心

三、使用方法

3.1引用命名空间

3.2使用示例

一、实现效果 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内容操作内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!

推荐阅读

    wps如何修改页眉上边距

    wps如何修改页眉上边距,WPS教程,1.WPS版word怎样设置页眉边距WPS中word设置页眉边距技巧“页边距”:页面上打印区域之外的空白空间。1、设

    不要混淆核心i7/i5/i3的名字细节

    不要混淆核心i7/i5/i3的名字细节,,Core i7 / i5 / i3处理器lga1156接口英特尔新的命名混乱,很多读者会感到困惑。甚至很多高级硬件玩家无法

    PC计算机:AMDCPU核心细节

    PC计算机:AMDCPU核心细节,,核心提示:AthlonXP的核心型athlonxp有4种不同的核心类型,但都有个共同点:他们都使用socketa接口,他们都使用PR标称值

    针灸科设置|针灸科常见操作

    针灸科设置|针灸科常见操作,,1. 针灸科常见操作⒈临床实践技能(临床实际本领)考核 ⑴基本操作:①中医四诊、针灸、推拿、拔罐等中医临床技术;

    128mb集双核心3000元办公配置

    128mb集双核心3000元办公配置,,支持双内存规格,集成的Radeon hd4200核心七c.a785g双显示彩虹V14主板现在的价格仅为499元,是办公用户攒机的

    如何解决errtor加载操作系统的引导

    如何解决errtor加载操作系统的引导,,电脑启动后,屏幕上出现了errtor加载操作system错误提示,无法继续启动进入系统。以下是diskedit软件解决