C#扩展一般用于linq
2024-10-11 18:37:45
下面是dictionary的扩展
using System.Collections.Generic; namespace NetAnalysis.Common
{ public static class DictionaryExtensionMethodClass
{
/// <summary>
/// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常
/// </summary>
public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
if (dict.ContainsKey(key) == false)
dict.Add(key, value);
return dict;
} /// <summary>
/// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换
/// </summary>
public static Dictionary<TKey, TValue> AddOrPeplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
dict[key] = value;
return dict;
} /// <summary>
/// 获取与指定的键相关联的值,如果没有则返回输入的默认值
/// </summary>
public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue)
{
return dict.ContainsKey(key) ? dict[key] : defaultValue;
} /// <summary>
/// 向字典中批量添加键值对
/// </summary>
/// <param name="replaceExisted">如果已存在,是否替换</param>
public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted)
{
foreach (var item in values)
{
if (dict.ContainsKey(item.Key) == false || replaceExisted)
dict[item.Key] = item.Value;
}
return dict; } }
}
最新文章
- Js 日期 多少分钟前,多少秒前
- UISegmentedControl 的使用
- MySql MyISAM和InnoDB的区别
- [转]matlab如何复制spectrum scope的图
- MongoDB 性能优化五个简单步骤
- Agri-Net poj 1258
- JS 精粹(一)
- Django+xadmin打造在线教育平台(十)
- RecyclerFlexboxLayoutManagerDemo【使用FlexboxLayoutManager实现流式布局】
- layui table分页 page为false时,limit问题
- java 查找类的所有子类
- 设计模式のCompositePattern(组合模式)----结构模式
- 关于Oracle数据库后台进程
- 新建node工程
- 异步多线程 Thread ThreadPool Task
- 如何在 Azure 中自定义 Windows 虚拟机
- Sql Server中的谓词和运算符
- 【js】replace()
- C#两个日期范围内的间隔
- 转载:爬虫技术浅析(Python)