<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
①新增資訊到字典中;
②根據鍵獲取值;
③根據值獲取鍵;
④修改指定鍵的值;
⑤修改指定值為相同資訊;
⑥根據鍵移除資訊;
⑦根據值移除資訊;
/*** * 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 }
using Utils;
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} 條rn"); } //顯示列表資訊 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} 條rn"); } }//Class_end }
到此這篇關於詳解C#對Dictionary內容的通用操作的文章就介紹到這了,更多相關C# Dictionary內容操作內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45