下面是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; } }
}

最新文章

  1. Js 日期 多少分钟前,多少秒前
  2. UISegmentedControl 的使用
  3. MySql MyISAM和InnoDB的区别
  4. [转]matlab如何复制spectrum scope的图
  5. MongoDB 性能优化五个简单步骤
  6. Agri-Net poj 1258
  7. JS 精粹(一)
  8. Django+xadmin打造在线教育平台(十)
  9. RecyclerFlexboxLayoutManagerDemo【使用FlexboxLayoutManager实现流式布局】
  10. layui table分页 page为false时,limit问题
  11. java 查找类的所有子类
  12. 设计模式のCompositePattern(组合模式)----结构模式
  13. 关于Oracle数据库后台进程
  14. 新建node工程
  15. 异步多线程 Thread ThreadPool Task
  16. 如何在 Azure 中自定义 Windows 虚拟机
  17. Sql Server中的谓词和运算符
  18. 【js】replace()
  19. C#两个日期范围内的间隔
  20. 转载:爬虫技术浅析(Python)

热门文章

  1. vim编辑python脚本时Tab补全
  2. [C++] 头文件中的#ifndef,#define,#endif以及#pragma用法
  3. selenium-04-验证码问题
  4. android studio 开发环境的搭建
  5. zookeeper 都有哪些使用场景?
  6. Linux 文件复制命令cp
  7. idea 自动生成并跳转单元测试
  8. 【爬虫小程序:爬取斗鱼所有房间信息】Xpath(协程池版)
  9. 深入了解String,StringBuffer和StringBuilder三个类的异同
  10. 列表 元祖 range