详解C#对Dictionary内容的通用操作
短信预约 -IT技能 免费直播动态提醒
一、实现效果
1.1实现的功能
①添加信息到字典中;
②根据键获取值;
③根据值获取键;
④修改指定键的值;
⑤修改指定值为相同信息;
⑥根据键移除信息;
⑦根据值移除信息;
1.2实现的功能效果图
二、实现核心
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内容操作内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341