public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
: this(comparer)
{
- if (collection == null) throw new ArgumentNullException("collection");
+ if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
InitializeFromCollection(collection);
}
int concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
: this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer)
{
- if (collection == null) throw new ArgumentNullException("collection");
- if (comparer == null) throw new ArgumentNullException("comparer");
+ if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+ if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
InitializeFromCollection(collection);
}
TValue dummy;
foreach (KeyValuePair<TKey, TValue> pair in collection)
{
- if (pair.Key == null) throw new ArgumentNullException("key");
+ if (pair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
if (!TryAddInternal(pair.Key, pair.Value, false, false, out dummy))
{
- throw new ArgumentException(GetResource("ConcurrentDictionary_SourceContainsDuplicateKeys"));
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_SourceContainsDuplicateKeys);
}
}
{
if (concurrencyLevel < 1)
{
- throw new ArgumentOutOfRangeException("concurrencyLevel", GetResource("ConcurrentDictionary_ConcurrencyLevelMustBePositive"));
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.concurrencyLevel, ExceptionResource.ConcurrentDictionary_ConcurrencyLevelMustBePositive);
}
if (capacity < 0)
{
- throw new ArgumentOutOfRangeException("capacity", GetResource("ConcurrentDictionary_CapacityMustNotBeNegative"));
+ ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ConcurrentDictionary_CapacityMustNotBeNegative);
}
- if (comparer == null) throw new ArgumentNullException("comparer");
+ if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
// any buckets.
/// contains too many elements.</exception>
public bool TryAdd(TKey key, TValue value)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue dummy;
return TryAddInternal(key, value, false, true, out dummy);
}
/// (Nothing in Visual Basic).</exception>
public bool ContainsKey(TKey key)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue throwAwayValue;
return TryGetValue(key, out throwAwayValue);
/// (Nothing in Visual Basic).</exception>
public bool TryRemove(TKey key, out TValue value)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
return TryRemoveInternal(key, out value, false, default(TValue));
}
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
public bool TryGetValue(TKey key, out TValue value)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
int bucketNo, lockNoUnused;
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
IEqualityComparer<TValue> valueComparer = EqualityComparer<TValue>.Default;
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
- if (array == null) throw new ArgumentNullException("array");
- if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
+ if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
int locksAcquired = 0;
try
if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
{
- throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
}
CopyToPairs(array, index);
TValue value;
if (!TryGetValue(key, out value))
{
- throw new KeyNotFoundException();
+ ThrowHelper.ThrowKeyNotFoundException();
}
return value;
}
set
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue dummy;
TryAddInternal(key, value, true, true, out dummy);
}
/// if the key was not in the dictionary.</returns>
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
- if (key == null) throw new ArgumentNullException("key");
- if (valueFactory == null) throw new ArgumentNullException("valueFactory");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+ if (valueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.valueFactory);
TValue resultingValue;
if (TryGetValue(key, out resultingValue))
/// key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
public TValue GetOrAdd(TKey key, TValue value)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue resultingValue;
TryAddInternal(key, value, false, true, out resultingValue);
/// absent) or the result of updateValueFactory (if the key was present).</returns>
public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
{
- if (key == null) throw new ArgumentNullException("key");
- if (addValueFactory == null) throw new ArgumentNullException("addValueFactory");
- if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+ if (addValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.addValueFactory);
+ if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
TValue newValue, resultingValue;
while (true)
/// absent) or the result of updateValueFactory (if the key was present).</returns>
public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
{
- if (key == null) throw new ArgumentNullException("key");
- if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+ if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
TValue newValue, resultingValue;
while (true)
{
{
if (!TryAdd(key, value))
{
- throw new ArgumentException(GetResource("ConcurrentDictionary_KeyAlreadyExisted"));
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_KeyAlreadyExisted);
}
}
/// name="keyValuePair"/> is a null reference (Nothing in Visual Basic).</exception>
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
{
- if (keyValuePair.Key == null) throw new ArgumentNullException(GetResource("ConcurrentDictionary_ItemKeyIsNull"));
-
+ if (keyValuePair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionResource.ConcurrentDictionary_ItemKeyIsNull);
TValue throwAwayValue;
return TryRemoveInternal(keyValuePair.Key, out throwAwayValue, true, keyValuePair.Value);
}
/// </exception>
void IDictionary.Add(object key, object value)
{
- if (key == null) throw new ArgumentNullException("key");
- if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+ if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
- TValue typedValue;
+ TValue typedValue = default(TValue);
try
{
typedValue = (TValue)value;
}
catch (InvalidCastException)
{
- throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
}
((IDictionary<TKey, TValue>)this).Add((TKey)key, typedValue);
/// (Nothing in Visual Basic).</exception>
bool IDictionary.Contains(object key)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
return (key is TKey) && ((ConcurrentDictionary<TKey, TValue>)this).ContainsKey((TKey)key);
}
/// (Nothing in Visual Basic).</exception>
void IDictionary.Remove(object key)
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue throwAwayValue;
if (key is TKey)
{
get
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
TValue value;
if (key is TKey && this.TryGetValue((TKey)key, out value))
}
set
{
- if (key == null) throw new ArgumentNullException("key");
+ if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
- if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
- if (!(value is TValue)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
+ if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
+ if (!(value is TValue)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
((ConcurrentDictionary<TKey, TValue>)this)[(TKey)key] = (TValue)value;
}
[SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
void ICollection.CopyTo(Array array, int index)
{
- if (array == null) throw new ArgumentNullException("array");
- if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
+ if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+ if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
int locksAcquired = 0;
try
if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
{
- throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
}
// To be consistent with the behavior of ICollection.CopyTo() in Dictionary<TKey,TValue>,
return;
}
- throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayIncorrectType"), "array");
+ ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayIncorrectType, ExceptionArgument.array);
}
finally
{
{
get
{
- throw new NotSupportedException(Environment.GetResourceString("ConcurrentCollection_SyncRoot_NotSupported"));
+ ThrowHelper.ThrowNotSupportedException(ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported);
+ return default(object);
}
}
}
/// <summary>
- /// A helper function to obtain the string for a particular resource key.
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- private string GetResource(string key)
- {
- Assert(key != null);
-
- return Environment.GetResourceString(key);
- }
-
- /// <summary>
/// A node in a singly-linked list representing a particular hash table bucket.
/// </summary>
private class Node