@@ -229,7 +229,7 @@ public ConcurrentDictionary(IEqualityComparer<TKey> comparer) : this(DefaultConc
229
229
public ConcurrentDictionary ( IEnumerable < KeyValuePair < TKey , TValue > > collection , IEqualityComparer < TKey > comparer )
230
230
: this ( comparer )
231
231
{
232
- if ( collection == null ) throw new ArgumentNullException ( " collection" ) ;
232
+ if ( collection == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . collection ) ;
233
233
234
234
InitializeFromCollection ( collection ) ;
235
235
}
@@ -259,8 +259,8 @@ public ConcurrentDictionary(
259
259
int concurrencyLevel , IEnumerable < KeyValuePair < TKey , TValue > > collection , IEqualityComparer < TKey > comparer )
260
260
: this ( concurrencyLevel , DEFAULT_CAPACITY , false , comparer )
261
261
{
262
- if ( collection == null ) throw new ArgumentNullException ( " collection" ) ;
263
- if ( comparer == null ) throw new ArgumentNullException ( " comparer" ) ;
262
+ if ( collection == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . collection ) ;
263
+ if ( comparer == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . comparer ) ;
264
264
265
265
InitializeFromCollection ( collection ) ;
266
266
}
@@ -270,11 +270,11 @@ private void InitializeFromCollection(IEnumerable<KeyValuePair<TKey, TValue>> co
270
270
TValue dummy ;
271
271
foreach ( KeyValuePair < TKey , TValue > pair in collection )
272
272
{
273
- if ( pair . Key == null ) throw new ArgumentNullException ( " key" ) ;
273
+ if ( pair . Key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
274
274
275
275
if ( ! TryAddInternal ( pair . Key , pair . Value , false , false , out dummy ) )
276
276
{
277
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_SourceContainsDuplicateKeys" ) ) ;
277
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_SourceContainsDuplicateKeys ) ;
278
278
}
279
279
}
280
280
@@ -312,13 +312,13 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
312
312
{
313
313
if ( concurrencyLevel < 1 )
314
314
{
315
- throw new ArgumentOutOfRangeException ( " concurrencyLevel" , GetResource ( " ConcurrentDictionary_ConcurrencyLevelMustBePositive" ) ) ;
315
+ ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . concurrencyLevel , ExceptionResource . ConcurrentDictionary_ConcurrencyLevelMustBePositive ) ;
316
316
}
317
317
if ( capacity < 0 )
318
318
{
319
- throw new ArgumentOutOfRangeException ( " capacity" , GetResource ( " ConcurrentDictionary_CapacityMustNotBeNegative" ) ) ;
319
+ ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . capacity , ExceptionResource . ConcurrentDictionary_CapacityMustNotBeNegative ) ;
320
320
}
321
- if ( comparer == null ) throw new ArgumentNullException ( " comparer" ) ;
321
+ if ( comparer == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . comparer ) ;
322
322
323
323
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
324
324
// any buckets.
@@ -358,7 +358,7 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
358
358
/// contains too many elements.</exception>
359
359
public bool TryAdd ( TKey key , TValue value )
360
360
{
361
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
361
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
362
362
TValue dummy ;
363
363
return TryAddInternal ( key , value , false , true , out dummy ) ;
364
364
}
@@ -375,7 +375,7 @@ public bool TryAdd(TKey key, TValue value)
375
375
/// (Nothing in Visual Basic).</exception>
376
376
public bool ContainsKey ( TKey key )
377
377
{
378
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
378
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
379
379
380
380
TValue throwAwayValue ;
381
381
return TryGetValue ( key , out throwAwayValue ) ;
@@ -395,7 +395,7 @@ public bool ContainsKey(TKey key)
395
395
/// (Nothing in Visual Basic).</exception>
396
396
public bool TryRemove ( TKey key , out TValue value )
397
397
{
398
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
398
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
399
399
400
400
return TryRemoveInternal ( key , out value , false , default ( TValue ) ) ;
401
401
}
@@ -486,7 +486,7 @@ private bool TryRemoveInternal(TKey key, out TValue value, bool matchValue, TVal
486
486
[ SuppressMessage ( "Microsoft.Concurrency" , "CA8001" , Justification = "Reviewed for thread safety" ) ]
487
487
public bool TryGetValue ( TKey key , out TValue value )
488
488
{
489
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
489
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
490
490
491
491
int bucketNo , lockNoUnused ;
492
492
@@ -531,7 +531,7 @@ public bool TryGetValue(TKey key, out TValue value)
531
531
[ SuppressMessage ( "Microsoft.Concurrency" , "CA8001" , Justification = "Reviewed for thread safety" ) ]
532
532
public bool TryUpdate ( TKey key , TValue newValue , TValue comparisonValue )
533
533
{
534
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
534
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
535
535
536
536
IEqualityComparer < TValue > valueComparer = EqualityComparer < TValue > . Default ;
537
537
@@ -642,8 +642,8 @@ public void Clear()
642
642
[ SuppressMessage ( "Microsoft.Concurrency" , "CA8001" , Justification = "ConcurrencyCop just doesn't know about these locks" ) ]
643
643
void ICollection < KeyValuePair < TKey , TValue > > . CopyTo ( KeyValuePair < TKey , TValue > [ ] array , int index )
644
644
{
645
- if ( array == null ) throw new ArgumentNullException ( " array" ) ;
646
- if ( index < 0 ) throw new ArgumentOutOfRangeException ( " index" , GetResource ( " ConcurrentDictionary_IndexIsNegative" ) ) ;
645
+ if ( array == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . array ) ;
646
+ if ( index < 0 ) ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . index , ExceptionResource . ConcurrentDictionary_IndexIsNegative ) ;
647
647
648
648
int locksAcquired = 0 ;
649
649
try
@@ -659,7 +659,7 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]
659
659
660
660
if ( array . Length - count < index || count < 0 ) //"count" itself or "count + index" can overflow
661
661
{
662
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_ArrayNotLargeEnough" ) ) ;
662
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_ArrayNotLargeEnough ) ;
663
663
}
664
664
665
665
CopyToPairs ( array , index ) ;
@@ -956,13 +956,13 @@ public TValue this[TKey key]
956
956
TValue value ;
957
957
if ( ! TryGetValue ( key , out value ) )
958
958
{
959
- throw new KeyNotFoundException ( ) ;
959
+ ThrowHelper . ThrowKeyNotFoundException ( ) ;
960
960
}
961
961
return value ;
962
962
}
963
963
set
964
964
{
965
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
965
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
966
966
TValue dummy ;
967
967
TryAddInternal ( key , value , true , true , out dummy ) ;
968
968
}
@@ -1026,8 +1026,8 @@ public int Count
1026
1026
/// if the key was not in the dictionary.</returns>
1027
1027
public TValue GetOrAdd ( TKey key , Func < TKey , TValue > valueFactory )
1028
1028
{
1029
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1030
- if ( valueFactory == null ) throw new ArgumentNullException ( " valueFactory" ) ;
1029
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1030
+ if ( valueFactory == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . valueFactory ) ;
1031
1031
1032
1032
TValue resultingValue ;
1033
1033
if ( TryGetValue ( key , out resultingValue ) )
@@ -1052,7 +1052,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
1052
1052
/// key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
1053
1053
public TValue GetOrAdd ( TKey key , TValue value )
1054
1054
{
1055
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1055
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1056
1056
1057
1057
TValue resultingValue ;
1058
1058
TryAddInternal ( key , value , false , true , out resultingValue ) ;
@@ -1080,9 +1080,9 @@ public TValue GetOrAdd(TKey key, TValue value)
1080
1080
/// absent) or the result of updateValueFactory (if the key was present).</returns>
1081
1081
public TValue AddOrUpdate ( TKey key , Func < TKey , TValue > addValueFactory , Func < TKey , TValue , TValue > updateValueFactory )
1082
1082
{
1083
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1084
- if ( addValueFactory == null ) throw new ArgumentNullException ( " addValueFactory" ) ;
1085
- if ( updateValueFactory == null ) throw new ArgumentNullException ( " updateValueFactory" ) ;
1083
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1084
+ if ( addValueFactory == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . addValueFactory ) ;
1085
+ if ( updateValueFactory == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . updateValueFactory ) ;
1086
1086
1087
1087
TValue newValue , resultingValue ;
1088
1088
while ( true )
@@ -1127,8 +1127,8 @@ public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKe
1127
1127
/// absent) or the result of updateValueFactory (if the key was present).</returns>
1128
1128
public TValue AddOrUpdate ( TKey key , TValue addValue , Func < TKey , TValue , TValue > updateValueFactory )
1129
1129
{
1130
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1131
- if ( updateValueFactory == null ) throw new ArgumentNullException ( " updateValueFactory" ) ;
1130
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1131
+ if ( updateValueFactory == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . updateValueFactory ) ;
1132
1132
TValue newValue , resultingValue ;
1133
1133
while ( true )
1134
1134
{
@@ -1207,7 +1207,7 @@ void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
1207
1207
{
1208
1208
if ( ! TryAdd ( key , value ) )
1209
1209
{
1210
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_KeyAlreadyExisted" ) ) ;
1210
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_KeyAlreadyExisted ) ;
1211
1211
}
1212
1212
}
1213
1213
@@ -1340,8 +1340,7 @@ bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
1340
1340
/// name="keyValuePair"/> is a null reference (Nothing in Visual Basic).</exception>
1341
1341
bool ICollection < KeyValuePair < TKey , TValue > > . Remove ( KeyValuePair < TKey , TValue > keyValuePair )
1342
1342
{
1343
- if ( keyValuePair . Key == null ) throw new ArgumentNullException ( GetResource ( "ConcurrentDictionary_ItemKeyIsNull" ) ) ;
1344
-
1343
+ if ( keyValuePair . Key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionResource . ConcurrentDictionary_ItemKeyIsNull ) ;
1345
1344
TValue throwAwayValue ;
1346
1345
return TryRemoveInternal ( keyValuePair . Key , out throwAwayValue , true , keyValuePair . Value ) ;
1347
1346
}
@@ -1387,17 +1386,17 @@ IEnumerator IEnumerable.GetEnumerator()
1387
1386
/// </exception>
1388
1387
void IDictionary . Add ( object key , object value )
1389
1388
{
1390
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1391
- if ( ! ( key is TKey ) ) throw new ArgumentException ( GetResource ( " ConcurrentDictionary_TypeOfKeyIncorrect" ) ) ;
1389
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1390
+ if ( ! ( key is TKey ) ) ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_TypeOfKeyIncorrect ) ;
1392
1391
1393
- TValue typedValue ;
1392
+ TValue typedValue = default ( TValue ) ;
1394
1393
try
1395
1394
{
1396
1395
typedValue = ( TValue ) value ;
1397
1396
}
1398
1397
catch ( InvalidCastException )
1399
1398
{
1400
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_TypeOfValueIncorrect" ) ) ;
1399
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_TypeOfValueIncorrect ) ;
1401
1400
}
1402
1401
1403
1402
( ( IDictionary < TKey , TValue > ) this ) . Add ( ( TKey ) key , typedValue ) ;
@@ -1415,7 +1414,7 @@ void IDictionary.Add(object key, object value)
1415
1414
/// (Nothing in Visual Basic).</exception>
1416
1415
bool IDictionary . Contains ( object key )
1417
1416
{
1418
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1417
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1419
1418
1420
1419
return ( key is TKey ) && ( ( ConcurrentDictionary < TKey , TValue > ) this ) . ContainsKey ( ( TKey ) key ) ;
1421
1420
}
@@ -1475,7 +1474,7 @@ ICollection IDictionary.Keys
1475
1474
/// (Nothing in Visual Basic).</exception>
1476
1475
void IDictionary . Remove ( object key )
1477
1476
{
1478
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1477
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1479
1478
1480
1479
TValue throwAwayValue ;
1481
1480
if ( key is TKey )
@@ -1517,7 +1516,7 @@ object IDictionary.this[object key]
1517
1516
{
1518
1517
get
1519
1518
{
1520
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1519
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1521
1520
1522
1521
TValue value ;
1523
1522
if ( key is TKey && this . TryGetValue ( ( TKey ) key , out value ) )
@@ -1529,10 +1528,10 @@ object IDictionary.this[object key]
1529
1528
}
1530
1529
set
1531
1530
{
1532
- if ( key == null ) throw new ArgumentNullException ( " key" ) ;
1531
+ if ( key == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . key ) ;
1533
1532
1534
- if ( ! ( key is TKey ) ) throw new ArgumentException ( GetResource ( " ConcurrentDictionary_TypeOfKeyIncorrect" ) ) ;
1535
- if ( ! ( value is TValue ) ) throw new ArgumentException ( GetResource ( " ConcurrentDictionary_TypeOfValueIncorrect" ) ) ;
1533
+ if ( ! ( key is TKey ) ) ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_TypeOfKeyIncorrect ) ;
1534
+ if ( ! ( value is TValue ) ) ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_TypeOfValueIncorrect ) ;
1536
1535
1537
1536
( ( ConcurrentDictionary < TKey , TValue > ) this ) [ ( TKey ) key ] = ( TValue ) value ;
1538
1537
}
@@ -1563,8 +1562,8 @@ object IDictionary.this[object key]
1563
1562
[ SuppressMessage ( "Microsoft.Concurrency" , "CA8001" , Justification = "ConcurrencyCop just doesn't know about these locks" ) ]
1564
1563
void ICollection . CopyTo ( Array array , int index )
1565
1564
{
1566
- if ( array == null ) throw new ArgumentNullException ( " array" ) ;
1567
- if ( index < 0 ) throw new ArgumentOutOfRangeException ( " index" , GetResource ( " ConcurrentDictionary_IndexIsNegative" ) ) ;
1565
+ if ( array == null ) ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . array ) ;
1566
+ if ( index < 0 ) ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . index , ExceptionResource . ConcurrentDictionary_IndexIsNegative ) ;
1568
1567
1569
1568
int locksAcquired = 0 ;
1570
1569
try
@@ -1581,7 +1580,7 @@ void ICollection.CopyTo(Array array, int index)
1581
1580
1582
1581
if ( array . Length - count < index || count < 0 ) //"count" itself or "count + index" can overflow
1583
1582
{
1584
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_ArrayNotLargeEnough" ) ) ;
1583
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_ArrayNotLargeEnough ) ;
1585
1584
}
1586
1585
1587
1586
// To be consistent with the behavior of ICollection.CopyTo() in Dictionary<TKey,TValue>,
@@ -1611,7 +1610,7 @@ void ICollection.CopyTo(Array array, int index)
1611
1610
return ;
1612
1611
}
1613
1612
1614
- throw new ArgumentException ( GetResource ( " ConcurrentDictionary_ArrayIncorrectType" ) , " array" ) ;
1613
+ ThrowHelper . ThrowArgumentException ( ExceptionResource . ConcurrentDictionary_ArrayIncorrectType , ExceptionArgument . array ) ;
1615
1614
}
1616
1615
finally
1617
1616
{
@@ -1641,7 +1640,8 @@ object ICollection.SyncRoot
1641
1640
{
1642
1641
get
1643
1642
{
1644
- throw new NotSupportedException ( Environment . GetResourceString ( "ConcurrentCollection_SyncRoot_NotSupported" ) ) ;
1643
+ ThrowHelper . ThrowNotSupportedException ( ExceptionResource . ConcurrentCollection_SyncRoot_NotSupported ) ;
1644
+ return default ( object ) ;
1645
1645
}
1646
1646
}
1647
1647
@@ -1976,18 +1976,6 @@ private void Assert(bool condition)
1976
1976
Contract . Assert ( condition ) ;
1977
1977
}
1978
1978
1979
- /// <summary>
1980
- /// A helper function to obtain the string for a particular resource key.
1981
- /// </summary>
1982
- /// <param name="key"></param>
1983
- /// <returns></returns>
1984
- private string GetResource ( string key )
1985
- {
1986
- Assert ( key != null ) ;
1987
-
1988
- return Environment . GetResourceString ( key ) ;
1989
- }
1990
-
1991
1979
/// <summary>
1992
1980
/// A node in a singly-linked list representing a particular hash table bucket.
1993
1981
/// </summary>
0 commit comments