ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法
https://msdn.microsoft.com/zh-cn/library/ee378665(v=vs.110).aspx
此方法有一共有2个,现在只讨论其中一个
public TValue AddOrUpdate(
TKey key,
TValue addValue,
Func<TKey, TValue, TValue> updateValueFactory
)
Parameters 参数说明
key 参数名称 【此参数不允许为null】
Type: TKey 参数类型
The key to be added or whose value should be updated 需要添加或者更新的key
addValue 参数名称
Type: TValue 参数类型
The value to be added for an absent key 和新key匹配的value
updateValueFactory 参数名称 【此参数不允许为null】
Type: System.Func<TKey, TValue, TValue> 参数类型
The function used to generate a new value for an existing key based on the key's existing value 此函数用来基于已有的key的value来生成新的值
Return Value 返回值
Type: TValue 返回值的类型
The new value for the key. This will be either be addValue (if the key was absent) or the result of updateValueFactory (if the key was present).
key对应的新value,可能是新添加的,也可能是更新的
假定目前有一个ConcurrentDictionary<string, Dictionary<string, Color>> dictionary,需要给它添加新的值
方法1:使用委托,写一个符合委托签名的方法
private Dictionary<string,Color> Method(string deviceId,Dictionary<string,Color> dic)
{
return dic;
}
调用的时候, dictionary.AddOrUpdate(warningInfo.DeviceID, dic, Method); //将Method作为参数进行传递
方法2:使用匿名委托
dictionary.AddOrUpdate(warningInfo.DeviceID, dic, delegate(string deviceId,Dictionary<string,Color> dic1)
{
return dic1;
});
方法3:直接使用lambda表达式
dictionary.AddOrUpdate(warningInfo.DeviceID, dic, (key, value) => value);
假如有多个ConcurrentDictionary的变量,都需要使用AddOrUpdate函数,并且第三个参数updateValueFactory的逻辑相同【目前假设逻辑仅仅是返回Value的值】
可以将updateValueFactory抽象成一个泛型方法来使用
/// <summary>
/// GenericMethod类,用于存放泛型方法[但是这个类本身不是泛型的]
/// </summary>
public class GenericMethod
{
/// <summary>
/// System.Collections.Concurrent.ConcurrentDictionary类中AddOrUpdate方法中的第三个参数对应的一个泛型方法
/// 目前的AddOrUpdate方法中的第三个三处Func委托只需要返回Value就可以了,不需要做其他的处理
/// </summary>
/// 参数
/// <param name="key">第一个参数</param>
/// <param name="value">第二个参数</param>
/// 类型参数
/// <typeparam name="TKey">第一个参数的类型</typeparam>
/// <typeparam name="TValue">第二个参数的类型</typeparam>
/// <returns></returns>
public static TValue UpdateValueFactory<TKey, TValue>(TKey key, TValue value)
{
return value;
}
}
ConcurrentDictionary<TKey, TValue>的AddOrUpdate方法的更多相关文章
- C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典
ConcurrentDictionary<Tkey,Tvalue> Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...
- 线程安全集合 ConcurrentDictionary<TKey, TValue> 类
ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...
- .net源码分析 - ConcurrentDictionary<TKey, TValue>
List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...
- 都说ConcurrentDictionary<TKey, TValue>有陷阱
看这么几句解释(英文原帖): private static void ConcurrentDictionary() { var dict = new ConcurrentDictionary<i ...
- c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展
Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多 ...
- “线程安全的” Dictionary(TKey,TValue)
这是一篇翻译,专门介绍Dictionary线程安全问题,原文网址如下 http://www.grumpydev.com/2010/02/25/thread-safe-dictionarytkeytva ...
- Dictionary<Tkey.TValue>与SortedList
一.概述 表示Key/Value集合,可以添加删除元素,允许按Key来访问元素.是Hashtable的泛型等效类. 它需要一个相等实现来确定键是否相等,可以使用实现了IEqualityComparer ...
- .net源码分析 – Dictionary<TKey, TValue>
接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...
- .NET中Dictionary<TKey, TValue>浅析
.NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...
随机推荐
- Windows Form 分页。
其实功能实现很简单.我做的是一个通用的分页控件.项目时间很紧,可能有点粗糙.欢迎大家斧正.不说了直接贴代码吧. using System; using System.Collections.Gener ...
- inputstream和outputstream读写数据模板代码
//读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(b ...
- Could not find file '..\bin\hibernate.cfg.xml'解决方法:
web.config: 解决方法:
- java多线程状态转换
http://www.mamicode.com/info-detail-517008.html 相关资料链接 我觉得下面这张图总结的很好
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- 【BZOJ】【2729】【HNOI2012】排队
排列组合+高精度 Orz PoPoQQQ 引用题解: 嗯……学习了一下python= =懒的写高精了 /************************************************ ...
- ios开发之数据存取1-SQLite
iOS开发中常用的数据存取方式有: XML属性列表-PList NSKeyedArchiver 归档 Preference-偏好设置 SQLite3 Core Data-以面向对象的方式操作数据库SQ ...
- win8 任务栏不合并隐藏标题
让win8任务栏不合并,并且隐藏标题的办法: 效果如下: 首先让win8不合并任务栏 1.任务栏上点鼠标右键 -- "属性" 2."任务栏按钮"选择" ...
- 01-07-01【Nhibernate (版本3.3.1.4000) 出入江湖】并发控制
Nhibernate 并发控制 [1]悲观并发控制 正在使用数据的操作,加上锁,使用完后解锁释放资源. 使用场景:数据竞争激烈,锁的成本低于回滚事务的成本 缺点:阻塞,可能死锁 [2]乐观并发控制: ...
- java 几种常见的定时器
今天闲着没事就总结了一下在java中常用的几种定时器. 主要有3种java.util.Timer, ScheduledExecutorService和quartz 一.Timer 举个简单例子.每隔5 ...