本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

ConcurrentDictionary<TKey, TValue>.AddOrUpdate 方法 (TKey, TValue, Func<TKey, TValue, TValue>)

.NET Framework (current version)
 

如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中;如果该键已经存在,则通过使用指定的函数更新 ConcurrentDictionary<TKey, TValue> 中的键/值对。

命名空间:   System.Collections.Concurrent
程序集:  mscorlib(位于 mscorlib.dll)

public TValue AddOrUpdate(
	TKey key,
	TValue addValue,
	Func<TKey, TValue, TValue> updateValueFactory
)

参数

key
Type: TKey

要添加的键或应更新其值的键

addValue
Type: TValue

要为空缺键添加的值

updateValueFactory
Type: System.Func<TKey, TValue, TValue>

用于基于键的现有值为现有键生成新值的函数

返回值

Type: TValue

键的新值。 这将是 addValue(如果缺少键)或 updateValueFactory 的结果(如果存在键)。

Exception Condition
ArgumentNullException

keyupdateValueFactorynull

OverflowException

字典中已包含元素的最大数量(MaxValue)。

The following code example shows how to initialize an T:System.Collections.Concurrent.ConcurrentDictionary`2 and how to use the AddOrUpdate method to add an additional item to the collection, and update the existing items.

using System;
using System.Collections.Concurrent;

class CD_Ctor
{
    // Demonstrates: 
    //      ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity) 
    //      ConcurrentDictionary<TKey, TValue>[TKey] 
    static void Main()
    {
        // We know how many items we want to insert into the ConcurrentDictionary. 
        // So set the initial capacity to some prime number above that, to ensure that 
        // the ConcurrentDictionary does not need to be resized while initializing it. 
        int HIGHNUMBER = 64;
        int initialCapacity = 101;

        // The higher the concurrencyLevel, the higher the theoretical number of operations 
        // that could be performed concurrently on the ConcurrentDictionary.  However, global 
        // operations like resizing the dictionary take longer as the concurrencyLevel rises.  
        // For the purposes of this example, we'll compromise at numCores * 2. 
        int numProcs = Environment.ProcessorCount;
        int concurrencyLevel = numProcs * 2;

        // Construct the dictionary with the desired concurrencyLevel and initialCapacity
        ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity);

        // Initialize the dictionary 
        for (int i = 1; i <= HIGHNUMBER; i++) cd[i] = i * i;

        Console.WriteLine("The square of 23 is {0} (should be {1})", cd[23], 23 * 23);

        // Now iterate through, adding one to the end of the list. Existing items should be updated to be divided by their 
        // key  and a new item will be added that is the square of its key.
        for (int i = 1; i <= HIGHNUMBER + 1; i++)
          cd.AddOrUpdate(i, i * i, (k,v) => v / i);

        Console.WriteLine("The square root of 529 is {0} (should be {1})", cd[23], 529 / 23);
        Console.WriteLine("The square of 65 is {0} (should be {1})", cd[HIGHNUMBER + 1], ((HIGHNUMBER + 1) * (HIGHNUMBER + 1)));

    }
}

通用 Windows 平台
自 8 起可用
.NET Framework
自 4.0 起可用
可移植类库
可移植 .NET 平台 中受支持
Windows Phone
自 8.1 起可用
返回页首
显示: