字典工具-DictionaryUtil
# 简介
静态工具类 DictionaryUtil,是一个C#工具类,提供了丰富的字典(Dictionary)操作方法,包括获取指定键的值、将一个字典的键值对添加到另一个字典中、返回字典中键或值的集合、从字典中删除指定的键、以及查找具有最大值或最小值的键等。这些方法可以帮助用户更轻松地操作字典。
# EasyTool.DictionaryUtil 类
方法说明:
GetValueOrDefault
: 获取字典中指定键的值,如果字典中不存在该键,则返回默认值。AddRange
: 将一个字典的所有键值对添加到另一个字典中。GetKeys
: 返回字典中键的集合。GetValues
: 返回字典中值的集合。RemoveKeys
: 从字典中删除指定的键。GetKeyWithMaxValue
: 返回字典中具有最大值的键。GetKeyWithMinValue
: 返回字典中具有最小值的键。
# GetValueOrDefault-获取字典中指定键的值,如果字典中不存在该键,则返回默认值
/// <summary>
/// 获取字典中指定键的值,如果字典中不存在该键,则返回默认值
/// </summary>
/// <param name="dictionary">要获取值的字典</param>
/// <param name="key">要获取值的键</param>
/// <param name="defaultValue">如果字典中不存在该键,则返回的默认值</param>
/// <returns>指定键的值,如果字典中不存在该键,则返回默认值</returns>
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default)
# AddRange-将一个字典的所有键值对添加到另一个字典中
/// <summary>
/// 将一个字典的所有键值对添加到另一个字典中
/// </summary>
/// <param name="destination">要添加键值对的目标字典</param>
/// <param name="source">包含要添加键值对的源字典</param>
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> destination, IDictionary<TKey, TValue> source)
# GetKeys-返回字典中键的集合
/// <summary>
/// 返回字典中键的集合
/// </summary>
/// <param name="dictionary">要获取键的字典</param>
/// <returns>字典中所有键的集合</returns>
public static IEnumerable<TKey> GetKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
# GetValues-返回字典中值的集合
/// <summary>
/// 返回字典中值的集合
/// </summary>
/// <param name="dictionary">要获取值的字典</param>
/// <returns>字典中所有值的集合</returns>
public static IEnumerable<TValue> GetValues<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
# RemoveKeys-从字典中删除指定的键
/// <summary>
/// 从字典中删除指定的键
/// </summary>
/// <param name="dictionary">要删除键的字典</param>
/// <param name="keys">要删除的键的集合</param>
public static void RemoveKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
# GetKeyWithMaxValue-返回字典中具有最大值的键
/// <summary>
/// 返回字典中具有最大值的键
/// </summary>
/// <param name="dictionary">要查找键的字典</param>
/// <returns>具有最大值的键</returns>
public static TKey GetKeyWithMaxValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TValue : IComparable<TValue>
# GetKeyWithMinValue-返回字典中具有最小值的键
/// <summary>
/// 返回字典中具有最小值的键
/// </summary>
/// <param name="dictionary">要查找键的字典</param>
/// <returns>具有最小值的键</returns>
public static TKey GetKeyWithMinValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TValue : IComparable<TValue>
# 代码示例
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// 创建一个字典
Dictionary<string, int> scores = new Dictionary<string, int>()
{
{"Alice", 90},
{"Bob", 80},
{"Charlie", 70},
{"David", 60},
{"Eve", 50}
};
// 使用GetValueOrDefault方法获取键为"Alice"的值,如果不存在,则返回默认值-1
int aliceScore = scores.GetValueOrDefault("Alice", -1);
Console.WriteLine($"Alice's score is {aliceScore}.");
// 创建一个新字典
Dictionary<string, int> newScores = new Dictionary<string, int>()
{
{"Frank", 85},
{"George", 95}
};
// 使用AddRange方法将新字典的所有键值对添加到原字典中
scores.AddRange(newScores);
// 使用GetKeys方法返回字典中的所有键
IEnumerable<string> keys = scores.GetKeys();
Console.WriteLine("All keys in the dictionary:");
foreach (string key in keys)
{
Console.WriteLine(key);
}
// 使用GetValues方法返回字典中的所有值
IEnumerable<int> values = scores.GetValues();
Console.WriteLine("All values in the dictionary:");
foreach (int value in values)
{
Console.WriteLine(value);
}
// 使用RemoveKeys方法删除指定的键
scores.RemoveKeys(new List<string>() { "Eve", "Frank" });
// 使用GetKeyWithMaxValue方法返回具有最大值的键
string maxScoreName = scores.GetKeyWithMaxValue();
Console.WriteLine($"The person with the highest score is {maxScoreName}.");
// 使用GetKeyWithMinValue方法返回具有最小值的键
string minScoreName = scores.GetKeyWithMinValue();
Console.WriteLine($"The person with the lowest score is {minScoreName}.");
}
}
# 输出结果
Alice's score is 90.
All keys in the dictionary:
Alice
Bob
Charlie
David
Eve
Frank
George
All values in the dictionary:
90
80
70
60
50
85
95
The person with the highest score is George.
The person with the lowest score is Eve.
上次更新: 2023/04/26, 22:10:06