ConcurrentDictionary<TKey,TValue>クラス

ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>();

bool r1 = dictionary.TryAdd(1, "A"); // true
bool r2 = dictionary.TryAdd(2, "B"); // true
bool r3 = dictionary.TryAdd(2, "C"); // false
// [1, A], [2, B]

bool r4 = dictionary.TryUpdate(2, "X", "B"); // true
bool r5 = dictionary.TryUpdate(2, "Y", "C"); // false
// [1, A], [2, X]

string str1, str2;
bool r6 = dictionary.TryGetValue(2, out str1); // true, "X"
bool r7 = dictionary.TryGetValue(3, out str2); // false, null

// 無条件に書き込む
dictionary[2] = "Z";
dictionary[3] = "Z";
// [1, A], [2, Z], [3, Z]

// 無条件に読み込む
string str3 = dictionary[3]; // "Z"
string str4 = dictionary[4]; // KeyNotFoundException

コンストラクタ

public ConcurrentDictionary (
    int concurrencyLevel, // 並行性レベル (concurrency level)
    int capacity);        // 格納できる要素の初期数
ConcurrentDictionary<TKey,TValue>(Int32, Int32) - ConcurrentDictionary<TKey,TValue> Constructor (System.Collections.Concurrent) | Microsoft Learn

concurrencyLevelで、このコレクションを同時に更新するスレッドの推定数を指定します。これを省略する形式の既定値は、CPUの数です。

capacityを省略したときの既定値は31です。 ConcurrentDictionary<TKey,TValue>() - ConcurrentDictionary<TKey,TValue> Constructor (System.Collections.Concurrent) | Microsoft Learn ConcurrentDictionary - ConcurrentDictionary.cs

プロパティ

Item[TKey]

このプロパティからの取得はTryGetValue()と同じですが、失敗したときにはKeyNotFoundExceptionが投げられます。設定はTryAdd()に近いですが、キーが存在していたならば更新されます。this - ConcurrentDictionary.cs

キーが存在するとき、そのキーの値も用いて更新するならばAddOrUpdate()を用います。

メソッド

追加と削除
メソッド 機能
TryAdd(TKey, TValue) 指定したキーと値の追加を試みる
TryGetValue(TKey, TValue) 指定したキーの値の取得を試みる
TryUpdate(TKey, TValue, TValue) 指定したキーが指定の値に等しいならば、更新する
TryRemove(TKey, TValue) 指定したキーの削除を試みる
AddOrUpdate(TKey, Func<TKey,TValue>, Func<TKey,TValue,TValue>) 指定したキーが存在しないならば追加、存在するならば更新する
GetOrAdd(TKey, Func<TKey,TValue>) 指定したキーが存在しないならば追加、存在するならば取得する
   
ConcurrentDictionary の項目を追加および削除する | Microsoft Learn

TryAdd()

public bool TryAdd (TKey key, TValue value);
ConcurrentDictionary<TKey,TValue>.TryAdd(TKey, TValue) メソッド (System.Collections.Concurrent) | Microsoft Learn

追加できた場合はtrueが返されます。キーがすでに存在するために追加できなかった場合は、falseが返されます。

内部ではMonitor.Enter()でスレッド セーフとしています。TryAdd - ConcurrentDictionary.cs

TryGetValue()

public bool TryGetValue (TKey key, out TValue value);
ConcurrentDictionary<TKey,TValue>.TryGetValue(TKey, TValue) メソッド (System.Collections.Concurrent) | Microsoft Learn

keyが見つかった場合はtrueが返されます。

内部ではVolatile.Read()でスレッド セーフとしています。TryGetValue - ConcurrentDictionary.cs

TryUpdate()

public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue);
ConcurrentDictionary<TKey,TValue>.TryUpdate(TKey, TValue, TValue) メソッド (System.Collections.Concurrent) | Microsoft Learn

keyの値がcomparisonValueに等しく、newValueに置換できた場合はtrueが返されます。comparisonValueに等しくなかったりkeyが存在しなかったならばfalseが返され、更新されません。

ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>
{
    [1] = "a",
};

bool r1 = dictionary.TryUpdate(1, "A1", "a"); // true, [1, A1]
bool r2 = dictionary.TryUpdate(1, "A2", "a"); // false, [1, A1]
bool r3 = dictionary.TryUpdate(2, "B", "b");  // false

TryRemove()

public bool TryRemove (TKey key, out TValue value);
ConcurrentDictionary<TKey,TValue>.TryRemove メソッド (System.Collections.Concurrent) | Microsoft Learn

削除に成功するとそのときの値がvalueに格納され、trueが返されます。失敗するとvalueにはTValueの既定値が格納されます。

ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>
{
    [10] = "a",
    [20] = "b",
};

string value1, value2, value3;
dictionary.TryRemove(10, out value1); // true, "a"
dictionary.TryRemove(20, out value2); // true, "b"
dictionary.TryRemove(30, out value3); // false, null

AddOrUpdate()

public TValue AddOrUpdate (
    TKey key,
    TValue addValue,
    Func<TKey,TValue,TValue> updateValueFactory
    );
AddOrUpdate(TKey, TValue, Func<TKey,TValue,TValue>) - ConcurrentDictionary<TKey,TValue>.AddOrUpdate メソッド (System.Collections.Concurrent) | Microsoft Learn

keyが存在しないならばaddValueの値を持つキーが追加され、存在するならばupdateValueFactoryの戻り値に更新されます。そしてkeyの新しい値が返されます。

ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>
{
    [1] = "a",
};

string r1 = dictionary.AddOrUpdate(1, "A", (key, val) => "C"); // "C"が返される
// [1, C] (キーが存在するため、updateValueFactoryの戻り値に更新される)

string r2 = dictionary.AddOrUpdate(2, "B", (key, val) => "D"); // "B"が返される
// [1, C], [2, B] (キーが存在しないため、addValueの値を持つキーが追加される)
Microsoft Learnから検索