public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection)
: this (collection, EqualityComparer<TKey>.Default)
{
- foreach (KeyValuePair<TKey, TValue> pair in collection)
- Add (pair.Key, pair.Value);
}
public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
}
+ void CheckKey (TKey key)
+ {
+ if (key == null)
+ throw new ArgumentNullException ("key");
+ }
+
void Add (TKey key, TValue value)
{
while (!TryAdd (key, value));
public bool TryAdd (TKey key, TValue value)
{
+ CheckKey (key);
return internalDictionary.Insert (Hash (key), key, Make (key, value));
}
public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
{
+ CheckKey (key);
+ if (addValueFactory == null)
+ throw new ArgumentNullException ("addValueFactory");
+ if (updateValueFactory == null)
+ throw new ArgumentNullException ("updateValueFactory");
return internalDictionary.InsertOrUpdate (Hash (key),
key,
() => Make (key, addValueFactory (key)),
TValue AddOrUpdate (TKey key, TValue addValue, TValue updateValue)
{
+ CheckKey (key);
return internalDictionary.InsertOrUpdate (Hash (key),
key,
Make (key, addValue),
public bool TryGetValue (TKey key, out TValue value)
{
+ CheckKey (key);
KeyValuePair<TKey, TValue> pair;
bool result = internalDictionary.Find (Hash (key), key, out pair);
value = pair.Value;
public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue)
{
+ CheckKey (key);
return internalDictionary.CompareExchange (Hash (key), key, Make (key, newValue), (e) => e.Value.Equals (comparisonValue));
}
public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
{
+ CheckKey (key);
return internalDictionary.InsertOrGet (Hash (key), key, Make (key, default(TValue)), () => Make (key, valueFactory (key))).Value;
}
public TValue GetOrAdd (TKey key, TValue value)
{
+ CheckKey (key);
return internalDictionary.InsertOrGet (Hash (key), key, Make (key, value), null).Value;
}
public bool TryRemove (TKey key, out TValue value)
{
+ CheckKey (key);
KeyValuePair<TKey, TValue> data;
bool result = internalDictionary.Delete (Hash (key), key, out data);
value = data.Value;
public bool ContainsKey (TKey key)
{
+ CheckKey (key);
KeyValuePair<TKey, TValue> dummy;
return internalDictionary.Find (Hash (key), key, out dummy);
}