public static TValue GetOrAdd<TKey,TValue>(
this Dictionary<TKey,TValue> dictionary,
TKey key,
Func<TKey, TValue> valueFactory)
{
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = valueFactory(key);
dictionary.Add(key, value);
}
return value;
} public static TValue AddOrUpdate<TKey,TValue>(
this Dictionary<TKey,TValue> dictionary,
TKey key,
Func<TKey, TValue> addValueFactory,
Func<TKey, TValue, TValue> updateValueFactory)
{
TValue value;
if (dictionary.TryGetValue(key, out value))
{
value = updateValueFactory(key, value);
}
else
{
value = addValueFactory(key);
}
dictionary[key] = value;
return value;
}

Dictionary GetOrAdd的更多相关文章

  1. AutoMapper映射ExpressionTree

    问题描述 项目中使用AutoMapper进行VO&DTO&Entity的互相映射,但是默认Map方法不支持Expression的转换.如 Expression<Func<E ...

  2. 使用AutoMapper时Expression的转换

    此文章为转载:http://www.bubuko.com/infodetail-699735.html 参考链接: http://q.cnblogs.com/q/34480/   dudu有回复,其中 ...

  3. C# 的 Dictionary 寫入前應注意事項

    一個已上線.用戶龐大的系統,幾個月來第一次出現這個系統錯誤訊息 : 「已經加入含有相同索引鍵的項目」「已添加了具有相同键的项」An item with the same key has already ...

  4. ConcurrentDictionary 对决 Dictionary+Locking

    在 .NET 4.0 之前,如果我们需要在多线程环境下使用 Dictionary 类,除了自己实现线程同步来保证线程安全之外,我们没有其他选择. 很多开发人员肯定都实现过类似的线程安全方案,可能是通过 ...

  5. ConcurrentDictionary和Dictionary

    http://stackoverflow.com/questions/6739193/is-the-concurrentdictionary-thread-safe-to-the-point-that ...

  6. C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典

    ConcurrentDictionary<Tkey,Tvalue>  Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...

  7. 浅谈ConcurrentDictionary与Dictionary

    在.NET4.0之前,如果我们需要在多线程环境下使用Dictionary类,除了自己实现线程同步来保证线程安全外,我们没有其他选择.很多开发人员肯定都实现过类似的线程安全方案,可能是通过创建全新的线程 ...

  8. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  9. ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary

    AR.Global 文档 1:对象或属性: 名称 类型 说明 DG 对象 DataGrid操作对象 //datagrid集合,根据ID取出DataGrid对象,将Json当数组用. Items: ne ...

随机推荐

  1. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  2. matlab设定mex接驳的C/C++编译器

    使用C/C++编写核心算法,使用matlab调用算法.做上层封装,通常是提升效率并提供易用性的一个不错的选择. mex需要设定接驳的C/C++编译器,官方文档在这里:https://ww2.mathw ...

  3. HBase(五)HBase的API操作

    一.项目环境搭建 新建 Maven Project,新建项目后在 pom.xml 中添加依赖: <dependency> <groupId>org.apache.hbase&l ...

  4. Django实战(22):处理登录和注销

    我们已经可以在view函数中判断用户是否已经登录以及获取用户信息: if request.user.is_authenticated(): #判断用户是否已登录 user = request.user ...

  5. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  6. win7 fiddler报“Creation of the root certificate was not successful”的问题

    cd "C:\Program Files (x86)\Fiddler2" makecert.exe -r -ss my -n "CN=DO_NOT_TRUST_Fiddl ...

  7. 决策树 (decision tree)

    内容学习于 ApacheCN github 定义: 分类决策树模型是一种描述对实例进行分类的树形结构.决策树由结点(node)和有向边(directed edge)组成.结点有两种类型:内部结点(in ...

  8. Windows Installer服务总是自动关闭导致无法安装在win10上安装英伟达显卡驱动的解决方案

    你可以依次点击"开始→程序→附件→命令提示符",键入:msiexec /unregister, 然后再键入msiexec /regserver.应该就能解决. 更多的参考:How ...

  9. 迭代器模式 与 C# IEnumerator/IEnumerable

    Part1 迭代器模式 与 接口 IEnumerable IEnumerator interface IEnumerable { IEnumerator GetEnumerator(); } // 泛 ...

  10. Alter GDG limit

    //JOBCARD... //*-------------------------------------------------------------------* //* Alter GDG l ...