@@ -26,11 +26,11 @@ public static bool TryGetValueSynchronized<TKey, TValue>(
2626 this IDictionary < TKey , TValue > target ,
2727 TKey key , out TValue value )
2828 {
29- if ( target is null ) throw new NullReferenceException ( ) ;
29+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
3030 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
3131 Contract . EndContractBlock ( ) ;
3232
33- TValue result = default ;
33+ TValue result = default ! ;
3434 var success = ThreadSafety . SynchronizeRead ( target , key , ( ) =>
3535 ThreadSafety . SynchronizeRead ( target , ( ) =>
3636 target . TryGetValue ( key , out result )
@@ -47,7 +47,7 @@ public static bool TryGetValueSynchronized<TKey, TValue>(
4747 /// </summary>
4848 public static TValue GetValueSynchronized < TKey , TValue > ( this IDictionary < TKey , TValue > target , TKey key , bool throwIfNotExists = true )
4949 {
50- if ( target is null ) throw new NullReferenceException ( ) ;
50+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
5151 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
5252 Contract . EndContractBlock ( ) ;
5353
@@ -56,7 +56,7 @@ public static TValue GetValueSynchronized<TKey, TValue>(this IDictionary<TKey, T
5656 if ( ! exists && throwIfNotExists )
5757 throw new KeyNotFoundException ( key . ToString ( ) ) ;
5858
59- return exists ? value : default ;
59+ return exists ? value : default ! ;
6060 }
6161
6262
@@ -65,7 +65,8 @@ public static TValue GetValueSynchronized<TKey, TValue>(this IDictionary<TKey, T
6565 /// </summary>
6666 public static void RegisterSynchronized < T > ( this ICollection < T > target , T value )
6767 {
68- if ( target is null ) throw new NullReferenceException ( ) ;
68+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
69+ if ( value is null ) throw new ArgumentNullException ( nameof ( value ) ) ;
6970 Contract . EndContractBlock ( ) ;
7071
7172 ThreadSafety . SynchronizeReadWriteKeyAndObject ( target , value ,
@@ -82,12 +83,12 @@ public static void RegisterSynchronized<T>(this ICollection<T> target, T value)
8283 public static T AddOrUpdateSynchronized < TKey , T > ( this IDictionary < TKey , T > target , TKey key , T value ,
8384 Func < TKey , T , T > updateValueFactory )
8485 {
85- if ( target is null ) throw new NullReferenceException ( ) ;
86+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
8687 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
8788 if ( updateValueFactory is null ) throw new ArgumentNullException ( nameof ( updateValueFactory ) ) ;
8889 Contract . EndContractBlock ( ) ;
8990
90- T valueUsed = default ;
91+ T valueUsed = default ! ;
9192
9293 // First we get a lock on the key action which should prevent the individual action from changing..
9394 ThreadSafety . SynchronizeWrite ( target , key , ( ) =>
@@ -120,13 +121,13 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
120121 Func < TKey , T > newValueFactory ,
121122 Func < TKey , T , T > updateValueFactory )
122123 {
123- if ( target is null ) throw new NullReferenceException ( ) ;
124+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
124125 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
125126 if ( newValueFactory is null ) throw new ArgumentNullException ( nameof ( newValueFactory ) ) ;
126127 if ( updateValueFactory is null ) throw new ArgumentNullException ( nameof ( updateValueFactory ) ) ;
127128 Contract . EndContractBlock ( ) ;
128129
129- T valueUsed = default ;
130+ T valueUsed = default ! ;
130131
131132 // First we get a lock on the key action which should prevent the individual action from changing..
132133 ThreadSafety . SynchronizeWrite ( target , key , ( ) =>
@@ -141,7 +142,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
141142 ( ) =>
142143 valueUsed = target . AddOrUpdate ( key ,
143144 newValueFactory ,
144- ( k , o ) => o . Equals ( old ) && k . Equals ( key ) ? updateValue : updateValueFactory ( k , o )
145+ ( k , o ) => k ! . Equals ( key ) && ( o ? . Equals ( old ) ?? old == null ) ? updateValue : updateValueFactory ( k , o )
145146 ) ) ;
146147 }
147148 else
@@ -152,7 +153,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
152153 ThreadSafety . SynchronizeWrite ( target ,
153154 ( ) =>
154155 valueUsed = target . AddOrUpdate ( key ,
155- k => k . Equals ( key ) ? value : newValueFactory ( k ) ,
156+ k => k ! . Equals ( key ) ? value : newValueFactory ( k ) ,
156157 updateValueFactory
157158 ) ) ;
158159 }
@@ -163,7 +164,7 @@ public static T AddOrUpdateSynchronized<TKey, T>(this IDictionary<TKey, T> targe
163164
164165 public static void AddSynchronized < T > ( this ICollection < T > target , T value )
165166 {
166- if ( target is null ) throw new NullReferenceException ( ) ;
167+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
167168 ThreadSafety . SynchronizeWrite ( target , ( ) => target . Add ( value ) ) ;
168169 }
169170
@@ -185,7 +186,7 @@ public static void AddToSynchronized<TKey, TValue>(this IDictionary<TKey, IList<
185186 /// </summary>
186187 public static void EnsureDefaultSynchronized < TKey , T > ( this IDictionary < TKey , T > target , TKey key , T defaultValue )
187188 {
188- if ( target is null ) throw new NullReferenceException ( ) ;
189+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
189190 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
190191 Contract . EndContractBlock ( ) ;
191192
@@ -200,7 +201,7 @@ public static void EnsureDefaultSynchronized<TKey, T>(this IDictionary<TKey, T>
200201 public static void EnsureDefaultSynchronized < TKey , T > ( this IDictionary < TKey , T > target , TKey key ,
201202 Func < TKey , T > defaultValueFactory )
202203 {
203- if ( target is null ) throw new NullReferenceException ( ) ;
204+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
204205 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
205206 if ( defaultValueFactory is null ) throw new ArgumentNullException ( nameof ( defaultValueFactory ) ) ;
206207 Contract . EndContractBlock ( ) ;
@@ -223,12 +224,12 @@ public static T GetOrAddSynchronized<TKey, T>(
223224 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS ,
224225 bool throwsOnTimeout = true )
225226 {
226- if ( target is null ) throw new NullReferenceException ( ) ;
227+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
227228 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
228229 ValidateMillisecondsTimeout ( millisecondsTimeout ) ;
229230 Contract . EndContractBlock ( ) ;
230231
231- T result = default ;
232+ T result = default ! ;
232233 bool condition ( LockType lockType ) => ! target . TryGetValue ( key , out result ) ;
233234 void render ( )
234235 {
@@ -252,13 +253,13 @@ public static T GetOrAddSynchronized<TKey, T>(
252253 Func < TKey , T > valueFactory ,
253254 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS )
254255 {
255- if ( target is null ) throw new NullReferenceException ( ) ;
256+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
256257 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
257258 if ( valueFactory is null ) throw new ArgumentNullException ( nameof ( valueFactory ) ) ;
258259 ValidateMillisecondsTimeout ( millisecondsTimeout ) ;
259260 Contract . EndContractBlock ( ) ;
260261
261- T result = default ;
262+ T result = default ! ;
262263 // Note, the following sync read is on the TARGET and not the key. See below.
263264 bool condition ( LockType lockType ) => ! ThreadSafety . SynchronizeRead ( target , ( ) => target . TryGetValue ( key , out result ) ) ;
264265
@@ -294,7 +295,7 @@ public static bool TryAddSynchronized<TKey, T>(
294295 T value ,
295296 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS )
296297 {
297- if ( target is null ) throw new NullReferenceException ( ) ;
298+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
298299 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
299300 Contract . EndContractBlock ( ) ;
300301
@@ -323,7 +324,7 @@ public static bool TryAddSynchronized<TKey, T>(
323324 Func < T > valueFactory ,
324325 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS )
325326 {
326- if ( target is null ) throw new NullReferenceException ( ) ;
327+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
327328 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
328329 Contract . EndContractBlock ( ) ;
329330
@@ -351,7 +352,7 @@ public static bool TryRemoveSynchronized<TKey, T>(
351352 TKey key ,
352353 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS )
353354 {
354- if ( target is null ) throw new NullReferenceException ( ) ;
355+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
355356 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
356357 Contract . EndContractBlock ( ) ;
357358
@@ -376,14 +377,14 @@ public static bool TryRemoveSynchronized<TKey, T>(
376377 out T value ,
377378 int millisecondsTimeout = SYNC_TIMEOUT_DEFAULT_MILLISECONDS )
378379 {
379- if ( target is null ) throw new NullReferenceException ( ) ;
380+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
380381 if ( key is null ) throw new ArgumentNullException ( nameof ( key ) ) ;
381382 Contract . EndContractBlock ( ) ;
382383
383- value = default ;
384+ value = default ! ;
384385 var removed = false ;
385386 ThreadSafety . SynchronizeReadWriteKeyAndObject (
386- target , key , ref value ,
387+ target , key , ref value ! ,
387388 lockType => target . ContainsKey ( key ) ,
388389 ( ) =>
389390 {
0 commit comments