最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解C#對(duì)Dictionary內(nèi)容的通用操作

 更新時(shí)間:2022年06月29日 16:21:10   作者:牛奶咖啡13  
這篇文章主要為大家詳細(xì)介紹了C#對(duì)Dictionary內(nèi)容的一些通用操作,例如:根據(jù)鍵移除信息、根據(jù)值移除信息、根據(jù)鍵獲取值等,需要的可以參考一下

一、實(shí)現(xiàn)效果

1.1實(shí)現(xiàn)的功能

①添加信息到字典中;

②根據(jù)鍵獲取值;

③根據(jù)值獲取鍵;

④修改指定鍵的值;

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

⑥根據(jù)鍵移除信息;

⑦根據(jù)值移除信息;

1.2實(shí)現(xiàn)的功能效果圖

二、實(shí)現(xiàn)核心

/***
*	Title:"容器" 項(xiàng)目
*		主題:Dictionary的幫助類
*	Description:
*		功能:
*		    ①添加信息到字典中
*		    ②根據(jù)鍵獲取值
*		    ③根據(jù)值獲取鍵
*		    ④修改指定鍵的值
*		    ⑤修改指定值為相同信息
*		    ⑥根據(jù)鍵移除信息
*		    ⑦根據(jù)值移除信息
*	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>
        /// 根據(jù)鍵獲取值
        /// </summary>
        /// <typeparam name="TKey">鍵類型</typeparam>
        /// <typeparam name="TValue">值類型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="key">鍵</param>
        /// <returns>返回鍵對(duì)應(yīng)的值</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>
        /// 根據(jù)值獲取鍵
        /// </summary>
        /// <typeparam name="TKey">鍵類型</typeparam>
        /// <typeparam name="TValue">值類型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="value">值</param>
        /// <returns>返回值對(duì)應(yīng)的所有鍵</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>返回修改結(jié)果(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>
        /// 根據(jù)鍵移除信息
        /// </summary>
        /// <typeparam name="TKey">鍵類型</typeparam>
        /// <typeparam name="TValue">值類型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needDeleteKey">需要?jiǎng)h除的鍵</param>
        /// <returns>返回移除結(jié)果(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>
        /// 根據(jù)值移除信息
        /// </summary>
        /// <typeparam name="TKey">鍵類型</typeparam>
        /// <typeparam name="TValue">值類型</typeparam>
        /// <param name="dic">字典</param>
        /// <param name="needDeleteValue">需要?jiǎng)h除的值</param>
        /// <returns>返回結(jié)果(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);
 
            //根據(jù)鍵獲取到對(duì)應(yīng)的值
            string queryKey = "L1";
            Console.WriteLine($"當(dāng)前查詢的鍵是:{queryKey}");
            string tmpValue = DictionaryHelper.GetValueOfKey(dic,queryKey);
            Console.WriteLine($"2-獲取到——鍵:L1對(duì)應(yīng)的值是:{tmpValue}");
 
            //根據(jù)值獲取到對(duì)應(yīng)的所有鍵
            string queryValue = "23.4";
            Console.WriteLine($"當(dāng)前查詢的值是:{queryValue}");
            List<string> tmpKey = DictionaryHelper.GetKeyOfValue(dic, queryValue);
            ShowInfoOfList(tmpKey);
 
            //修改指定鍵的值
            string needModifyKey = "L4";
            string replaceValue1 = "66";
            Console.WriteLine($"當(dāng)前需要修改的鍵是:{needModifyKey}_替換為的值是:{replaceValue1}");
            DictionaryHelper.ModifyInfoOfKey(dic, needModifyKey, replaceValue1);
            Console.WriteLine($"修改的鍵是:{needModifyKey}_替換為的值是:{replaceValue1}后所有內(nèi)容如下:");
            ShowInfoOfDic(dic);
 
            //修改指定值為相同信息
            string needModifyValue = "23.6";
            string replaceValue = "33.9";
            Console.WriteLine($"當(dāng)前需要修改的值是:{needModifyValue}_替換為的值是:{replaceValue}");
            DictionaryHelper.ModifyInfoOfValue(dic,needModifyValue,replaceValue);
            Console.WriteLine($"修改的值是:{needModifyValue}_替換為的值是:{replaceValue}后所有內(nèi)容如下:");
            ShowInfoOfDic(dic);
 
            //根據(jù)鍵移除信息
            string curRemoveKey = "L3";
            Console.WriteLine($"當(dāng)前移除的鍵是:{curRemoveKey}");
            DictionaryHelper.RemoveInfoOfKey(dic,curRemoveKey);
            Console.WriteLine($"移除的鍵是:{curRemoveKey}后所有內(nèi)容如下:");
            ShowInfoOfDic(dic);
 
            //根據(jù)值移除信息
            string curRemoveValue = "23.4";
            Console.WriteLine($"當(dāng)前移除的值是:{curRemoveValue}");
            DictionaryHelper.RemoveInfoOfValue(dic, curRemoveValue);
            Console.WriteLine($"移除的值是:{curRemoveValue}后所有內(nèi)容如下:");
            ShowInfoOfDic(dic);
 
            Console.ReadLine();
        }
 
        //獲取一個(gè)字典
        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($"--------------顯示信息完成______當(dāng)前字典:{dic.GetType().Name} 共有數(shù)據(jù):{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($"對(duì)應(yīng)內(nèi)容:{item}");
            }
            Console.WriteLine($"--------------顯示信息完成______當(dāng)前列表:{list.GetType().Name} 共有數(shù)據(jù):{list.Count} 條\r\n");
        }
 
    }//Class_end
}

到此這篇關(guān)于詳解C#對(duì)Dictionary內(nèi)容的通用操作的文章就介紹到這了,更多相關(guān)C# Dictionary內(nèi)容操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#后臺(tái)調(diào)用前臺(tái)JS函數(shù)方法

    C#后臺(tái)調(diào)用前臺(tái)JS函數(shù)方法

    今天小編就為大家分享一篇關(guān)于C#后臺(tái)調(diào)用前臺(tái)JS函數(shù)方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • unityZXing二維碼的生成與掃描

    unityZXing二維碼的生成與掃描

    這篇文章主要為大家詳細(xì)介紹了unityZXing二維碼的生成與掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • C# HTTP認(rèn)證方式詳解與代碼實(shí)現(xiàn)

    C# HTTP認(rèn)證方式詳解與代碼實(shí)現(xiàn)

    在C#中,HTTP認(rèn)證是客戶端與服務(wù)器之間進(jìn)行身份驗(yàn)證的一種機(jī)制,常見(jiàn)的HTTP認(rèn)證方式包括:Basic認(rèn)證、Digest認(rèn)證、OAuth、Bearer Token等,下面我們將從工作原理、優(yōu)缺點(diǎn)對(duì)比、代碼實(shí)現(xiàn)、案例實(shí)戰(zhàn)四個(gè)方面詳細(xì)介紹這些認(rèn)證方式,需要的朋友可以參考下
    2025-03-03
  • C#設(shè)計(jì)模式之Facade外觀模式解決天河城購(gòu)物問(wèn)題示例

    C#設(shè)計(jì)模式之Facade外觀模式解決天河城購(gòu)物問(wèn)題示例

    這篇文章主要介紹了C#設(shè)計(jì)模式之Facade外觀模式解決天河城購(gòu)物問(wèn)題,簡(jiǎn)單描述了外觀模式的定義并結(jié)合具體實(shí)例分析了外觀模式解決購(gòu)物問(wèn)題的相關(guān)步驟與操作技巧,需要的朋友可以參考下
    2017-09-09
  • C#中使用反射獲取結(jié)構(gòu)體實(shí)例及思路

    C#中使用反射獲取結(jié)構(gòu)體實(shí)例及思路

    一般用反射獲取類對(duì)象的實(shí)例比較簡(jiǎn)單,只要類有一個(gè)無(wú)參構(gòu)造函數(shù)或沒(méi)有顯示聲明帶參的構(gòu)造函數(shù)即可使用
    2013-10-10
  • 解析c# yield關(guān)鍵字

    解析c# yield關(guān)鍵字

    這篇文章主要介紹了c# yield關(guān)鍵字的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • C#中異步是4種實(shí)現(xiàn)方式詳解

    C#中異步是4種實(shí)現(xiàn)方式詳解

    這篇文章主要為大家詳細(xì)介紹了C#中異步實(shí)現(xiàn)方式的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Unity制作自定義字體的兩種方法

    Unity制作自定義字體的兩種方法

    這篇文章主要為大家詳細(xì)介紹了Unity制作自定義字體的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C# 元組和值元組的具體使用

    C# 元組和值元組的具體使用

    這篇文章主要介紹了C# 元組和值元組的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • C#結(jié)合OpenCVSharp4使用直方圖算法實(shí)現(xiàn)圖片相似度比較

    C#結(jié)合OpenCVSharp4使用直方圖算法實(shí)現(xiàn)圖片相似度比較

    這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合OpenCVSharp4使用直方圖算法實(shí)現(xiàn)圖片相似度比較,文中的示例代碼簡(jiǎn)潔易懂,需要的小伙伴可以參考下
    2023-09-09

最新評(píng)論

航空| 阿瓦提县| 南开区| 公主岭市| 宜黄县| 五常市| 吉隆县| 明光市| 南岸区| 二连浩特市| 凤阳县| 漠河县| 安化县| 佛山市| 轮台县| 连城县| 临澧县| 安福县| 曲阜市| 乌审旗| 晴隆县| 广水市| 太白县| 苍梧县| 涟水县| 永德县| 额敏县| 徐水县| 呈贡县| 基隆市| 微山县| 离岛区| 德令哈市| 龙泉市| 滨海县| 交口县| 和政县| 怀集县| 九江县| 道真| 那曲县|