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方法的更多相关文章

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

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

  2. 线程安全集合 ConcurrentDictionary<TKey, TValue> 类

    ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...

  3. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  4. 都说ConcurrentDictionary<TKey, TValue>有陷阱

    看这么几句解释(英文原帖): private static void ConcurrentDictionary() { var dict = new ConcurrentDictionary<i ...

  5. c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展

    Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多 ...

  6. “线程安全的” Dictionary(TKey,TValue)

    这是一篇翻译,专门介绍Dictionary线程安全问题,原文网址如下 http://www.grumpydev.com/2010/02/25/thread-safe-dictionarytkeytva ...

  7. Dictionary<Tkey.TValue>与SortedList

    一.概述 表示Key/Value集合,可以添加删除元素,允许按Key来访问元素.是Hashtable的泛型等效类. 它需要一个相等实现来确定键是否相等,可以使用实现了IEqualityComparer ...

  8. .net源码分析 – Dictionary<TKey, TValue>

    接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blo ...

  9. .NET中Dictionary<TKey, TValue>浅析

    .NET中Dictionary<TKey, Tvalue>是非常常用的key-value的数据结构,也就是其实就是传说中的哈希表..NET中还有一个叫做Hashtable的类型,两个类型都 ...

随机推荐

  1. php操作mysqli(示例代码)

    <?php define("MYSQL_OPEN_LOGS",true); class mysqliHelp { private $db; public function _ ...

  2. VS查看工程项目代码行数

    ctrl+shift+F  查找选项选正则表达式  正则表达式 b*[^:b#/]+.*$或 ^b*[^:b#/]+.*$

  3. Interview-Largest independent set in binary tree.

    BT(binary tree), want to find the LIS(largest independent set) of the BT. LIS: if the current node i ...

  4. Java 8 VM GC Tunning Guild Charter 9-b

    第九章 G1 GC The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for ...

  5. asp.net中XmlDocument解析出现出错,处理特殊字符

    xml结构会解析一些特殊字符,特别是& <  所以我们需要把结构放在CDATA中处理,CDATA里面的内容在XmlDocument 解析时会自动忽略掉,不会解析里面的内容:因此,我这里就 ...

  6. c++ std::bitset

    转载自 作用:及  64位 移位  取或  用64个位存储64个位,取 或 merge . 然后查索引即知道id是否存在~~ 目标:省空间. #include <iostream> #in ...

  7. mac 搭建git服务器

      一.简单搭建,不提供复杂的权限管理: 远程建立git用户,并打开ssh服务:见http://www.cnblogs.com/whj198579/archive/2013/04/09/3009350 ...

  8. UVA 12382 Grid of Lamps 贪心

    题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...

  9. Matlab找二维数组最大值

    1.m先max(x)求出每列最大值,返回行向量,再max对行向量求出最大值,就是max(max(x)). 注意:max(x),不管x是行列向量,只要是向量,那么就返回一个值. 2.先x(:)转为按列的 ...

  10. 【面试题013】在O(1)时间删除链表结点

    [面试题013]在O(1)时间删除链表结点  我们要删除结点i,我们可以把结点i的下一个结点j的内容复制到结点i,然后呢把结点i的指针指向结点j的下一个结点.然后在删除结点j. 1.如果结点i位于链表 ...