ConcurrentDictionary<TKey,TValue>.GetOrAdd ConcurrentDictionary<TKey,TValue>.GetOrAdd ConcurrentDictionary<TKey,TValue>.GetOrAdd ConcurrentDictionary<TKey,TValue>.GetOrAdd Method

定義

如果索引鍵不存在,則將索引鍵/值組加入 ConcurrentDictionary<TKey,TValue>Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. 傳回新的值,若索引鍵已存在,則傳回現有的值。Returns the new value, or the existing value if the key already exists.

多載

GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>)

如果索引鍵不存在,則使用指定的函式將索引鍵/值組加入 ConcurrentDictionary<TKey,TValue>Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function if the key does not already exist. 傳回新的值,若索引鍵已存在,則傳回現有的值。Returns the new value, or the existing value if the key exists.

GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue)

如果索引鍵不存在,則將索引鍵/值組加入 ConcurrentDictionary<TKey,TValue>Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. 傳回新的值,若索引鍵已存在,則傳回現有的值。Returns the new value, or the existing value if the key exists.

GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)

若機碼不存在,使用指定的函式與引數將機碼/值組新增到 ConcurrentDictionary<TKey,TValue>;若機碼存在,傳回現有的值。Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function and an argument if the key does not already exist, or returns the existing value if the key exists.

範例

下列範例示範如何呼叫GetOrAdd方法:The following example shows how to call the GetOrAdd method:

class CD_GetOrAddOrUpdate
{
    // Demonstrates:
    //      ConcurrentDictionary<TKey, TValue>.AddOrUpdate()
    //      ConcurrentDictionary<TKey, TValue>.GetOrAdd()
    //      ConcurrentDictionary<TKey, TValue>[]
    static void Main()
    {
        // Construct a ConcurrentDictionary
        ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>();

        // Bombard the ConcurrentDictionary with 10000 competing AddOrUpdates
        Parallel.For(0, 10000, i =>
        {
            // Initial call will set cd[1] = 1.  
            // Ensuing calls will set cd[1] = cd[1] + 1
            cd.AddOrUpdate(1, 1, (key, oldValue) => oldValue + 1);
        });

        Console.WriteLine("After 10000 AddOrUpdates, cd[1] = {0}, should be 10000", cd[1]);

        // Should return 100, as key 2 is not yet in the dictionary
        int value = cd.GetOrAdd(2, (key) => 100);
        Console.WriteLine("After initial GetOrAdd, cd[2] = {0} (should be 100)", value);

        // Should return 100, as key 2 is already set to that value
        value = cd.GetOrAdd(2, 10000);
        Console.WriteLine("After second GetOrAdd, cd[2] = {0} (should be 100)", value);
    }
}
' Imports System.Collections.Concurrent
' Imports System.Threading.Tasks
Class CD_GetOrAddOrUpdate

    ' Demonstrates:
    ' ConcurrentDictionary<TKey, TValue>.AddOrUpdate()
    ' ConcurrentDictionary<TKey, TValue>.GetOrAdd()
    ' ConcurrentDictionary<TKey, TValue>[]
    Shared Sub Main()
        ' Construct a ConcurrentDictionary
        Dim cd As New ConcurrentDictionary(Of Integer, Integer)()

        ' Bombard the ConcurrentDictionary with 10000 competing AddOrUpdates
        Parallel.For(0, 10000,
                       Sub(i)
                           ' Initial call will set cd[1] = 1. 
                           ' Ensuing calls will set cd[1] = cd[1] + 1
                           cd.AddOrUpdate(1, 1, Function(key, oldValue) oldValue + 1)
                       End Sub)

        Console.WriteLine("After 10000 AddOrUpdates, cd[1] = {0}, should be 10000", cd(1))

        ' Should return 100, as key 2 is not yet in the dictionary
        Dim value As Integer = cd.GetOrAdd(2, Function(key) 100)
        Console.WriteLine("After initial GetOrAdd, cd[2] = {0} (should be 100)", value)

        ' Should return 100, as key 2 is already set to that value
        value = cd.GetOrAdd(2, 10000)
        Console.WriteLine("After second GetOrAdd, cd[2] = {0} (should be 100)", value)
    End Sub
End Class

GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>) GetOrAdd(TKey, Func<TKey,TValue>)

如果索引鍵不存在,則使用指定的函式將索引鍵/值組加入 ConcurrentDictionary<TKey,TValue>Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function if the key does not already exist. 傳回新的值,若索引鍵已存在,則傳回現有的值。Returns the new value, or the existing value if the key exists.

public:
 TValue GetOrAdd(TKey key, Func<TKey, TValue> ^ valueFactory);
public TValue GetOrAdd (TKey key, Func<TKey,TValue> valueFactory);
member this.GetOrAdd : 'Key * Func<'Key, 'Value> -> 'Value
Public Function GetOrAdd (key As TKey, valueFactory As Func(Of TKey, TValue)) As TValue

參數

key
TKey TKey TKey TKey

要加入的項目的索引鍵。The key of the element to add.

valueFactory
Func<TKey,TValue>

用來為索引鍵產生值的函式。The function used to generate a value for the key.

傳回

TValue TValue TValue TValue

索引鍵的值。The value for the key. 這個值將是索引鍵的現有值 (如果字典中已有索引鍵),或是新值 (如果字典中沒有索引鍵)。This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.

例外狀況

keyvalueFactorynullkey or valueFactory is null.

字典已經包含項目數目上限 (MaxValue)。The dictionary already contains the maximum number of elements (MaxValue).

備註

修改,以及寫入至字典中,作業ConcurrentDictionary<TKey,TValue>使用更細緻的鎖定以確保執行緒安全性。For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (在字典上的讀取作業會在無鎖定的方式執行)。不過,valueFactory之外的鎖定,以避免發生的問題,可以從執行不明的程式碼,在鎖定下呼叫委派。(Read operations on the dictionary are performed in a lock-free manner.) However, the valueFactory delegate is called outside the locks to avoid the problems that can arise from executing unknown code under a lock. 因此,GetOrAdd不是關於所有其他作業的不可部分完成ConcurrentDictionary<TKey,TValue>類別。Therefore, GetOrAdd is not atomic with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class.

因為索引鍵/值可以由另一個執行緒時插入valueFactory會產生值,您不能信任,因為valueFactory執行,其所產生的值將會插入字典並傳回。Since a key/value can be inserted by another thread while valueFactory is generating a value, you cannot trust that just because valueFactory executed, its produced value will be inserted into the dictionary and returned. 如果您呼叫GetOrAdd同時在不同的執行緒上valueFactory可呼叫多次,但是只有一個索引鍵/值組會加入至字典。If you call GetOrAdd simultaneously on different threads, valueFactory may be called multiple times, but only one key/value pair will be added to the dictionary.

傳回的值取決於字典和索引鍵/值是否插入之後的另一個執行緒中的索引鍵是否存在GetOrAdd之前稱為valueFactory會產生一個值:The return value depends on the presence of the key in the dictionary and whether a key/value is inserted by another thread after GetOrAdd is called but before valueFactory generates a value:

情節Scenario 傳回值Return value
在字典中已有索引鍵。The key is already in the dictionary. 會傳回現有的值。The existing value is returned.
索引鍵不在字典中。The key is not in the dictionary. valueFactory 產生值。valueFactory generates a value. 在 重新檢查索引鍵,位於沒有索引鍵。On rechecking for the key, no key is found. 索引鍵/值會插入到字典,且會傳回值。The key/value is inserted into the dictionary, and the value is returned.
索引鍵不在字典中。The key is not in the dictionary. valueFactory 產生值。valueFactory generates a value. 雖然valueFactory是產生的值,不同的執行緒插入索引鍵的值。While valueFactory is generating the value, a different thread inserts a value for the key. 之後valueFactory執行時重新檢查索引鍵,另一個執行緒所插入的索引鍵找不到。After valueFactory executes and upon rechecking for the key, the key inserted by the other thread is found. 會傳回另一個執行緒插入的值。The value inserted by the other thread is returned.
另請參閱

GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue) GetOrAdd(TKey, TValue)

如果索引鍵不存在,則將索引鍵/值組加入 ConcurrentDictionary<TKey,TValue>Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> if the key does not already exist. 傳回新的值,若索引鍵已存在,則傳回現有的值。Returns the new value, or the existing value if the key exists.

public:
 TValue GetOrAdd(TKey key, TValue value);
public TValue GetOrAdd (TKey key, TValue value);
member this.GetOrAdd : 'Key * 'Value -> 'Value
Public Function GetOrAdd (key As TKey, value As TValue) As TValue

參數

key
TKey TKey TKey TKey

要加入的項目的索引鍵。The key of the element to add.

value
TValue TValue TValue TValue

索引鍵不存在時要加入的值。The value to be added, if the key does not already exist.

傳回

TValue TValue TValue TValue

索引鍵的值。The value for the key. 這個值將是索引鍵的現有值 (如果字典中已有索引鍵),或是新值 (如果字典中沒有索引鍵)。This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.

例外狀況

字典已經包含項目數目上限 (MaxValue)。The dictionary already contains the maximum number of elements (MaxValue).

另請參閱

GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg) GetOrAdd<TArg>(TKey, Func<TKey,TArg,TValue>, TArg)

若機碼不存在,使用指定的函式與引數將機碼/值組新增到 ConcurrentDictionary<TKey,TValue>;若機碼存在,傳回現有的值。Adds a key/value pair to the ConcurrentDictionary<TKey,TValue> by using the specified function and an argument if the key does not already exist, or returns the existing value if the key exists.

public:
generic <typename TArg>
 TValue GetOrAdd(TKey key, Func<TKey, TArg, TValue> ^ valueFactory, TArg factoryArgument);
public TValue GetOrAdd<TArg> (TKey key, Func<TKey,TArg,TValue> valueFactory, TArg factoryArgument);
member this.GetOrAdd : 'Key * Func<'Key, 'Arg, 'Value> * 'Arg -> 'Value
Public Function GetOrAdd(Of TArg) (key As TKey, valueFactory As Func(Of TKey, TArg, TValue), factoryArgument As TArg) As TValue

類型參數

TArg

將傳入的引數的型別valueFactoryThe type of an argument to pass into valueFactory.

參數

key
TKey TKey TKey TKey

要加入的項目的索引鍵。The key of the element to add.

valueFactory
Func<TKey,TArg,TValue>

用來為索引鍵產生值的函式。The function used to generate a value for the key.

factoryArgument

要傳入 valueFactory 的引數值。An argument value to pass into valueFactory.

傳回

TValue TValue TValue TValue

索引鍵的值。The value for the key. 這個值將是索引鍵的現有值 (如果字典中已有索引鍵),或是新值 (如果字典中沒有索引鍵)。This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary.

例外狀況

keynull參考 (在 Visual Basic 中 Nothing)。key is a null reference (Nothing in Visual Basic).

字典包含太多的項目。The dictionary contains too many elements.

備註

修改,以及寫入至字典中,作業ConcurrentDictionary<TKey,TValue>使用更細緻的鎖定以確保執行緒安全性。For modifications and write operations to the dictionary, ConcurrentDictionary<TKey,TValue> uses fine-grained locking to ensure thread safety. (在字典上的讀取作業會在無鎖定的方式執行)。不過,valueFactory之外的鎖定,以避免發生的問題,可以從執行不明的程式碼,在鎖定下呼叫委派。(Read operations on the dictionary are performed in a lock-free manner.) However, the valueFactory delegate is called outside the locks to avoid the problems that can arise from executing unknown code under a lock. 因此,GetOrAdd不是關於所有其他作業的不可部分完成ConcurrentDictionary<TKey,TValue>類別。Therefore, GetOrAdd is not atomic with regards to all other operations on the ConcurrentDictionary<TKey,TValue> class.

因為索引鍵/值可以由另一個執行緒時插入valueFactory會產生值,您不能信任,因為valueFactory執行,其所產生的值將會插入字典並傳回。Since a key/value can be inserted by another thread while valueFactory is generating a value, you cannot trust that just because valueFactory executed, its produced value will be inserted into the dictionary and returned. 如果您呼叫GetOrAdd同時在不同的執行緒上valueFactory可呼叫多次,但是只有一個索引鍵/值組會加入至字典。If you call GetOrAdd simultaneously on different threads, valueFactory may be called multiple times, but only one key/value pair will be added to the dictionary.

傳回的值取決於字典和索引鍵/值是否插入之後的另一個執行緒中的索引鍵是否存在GetOrAdd之前稱為valueFactory會產生一個值:The return value depends on the presence of the key in the dictionary and whether a key/value is inserted by another thread after GetOrAdd is called but before valueFactory generates a value:

情節Scenario 傳回值Return value
在字典中已有索引鍵。The key is already in the dictionary. 會傳回現有的值。The existing value is returned.
索引鍵不在字典中。The key is not in the dictionary. valueFactory 產生值。valueFactory generates a value. 在 重新檢查索引鍵,位於沒有索引鍵。On rechecking for the key, no key is found. 索引鍵/值會插入到字典,且會傳回值。The key/value is inserted into the dictionary, and the value is returned.
索引鍵不在字典中。The key is not in the dictionary. valueFactory 產生值。valueFactory generates a value. 雖然valueFactory是產生的值,不同的執行緒插入索引鍵的值。While valueFactory is generating the value, a different thread inserts a value for the key. 之後valueFactory執行時重新檢查索引鍵,另一個執行緒所插入的索引鍵找不到。After valueFactory executes and upon rechecking for the key, the key inserted by the other thread is found. 會傳回另一個執行緒插入的值。The value inserted by the other thread is returned.

適用於