@@ -271,11 +271,6 @@ public void Cleanup()
271
271
{
272
272
type2s . Add ( enumerator . Key ) ;
273
273
}
274
-
275
- #if ! NETSTANDARD2_1
276
- // Do the cleanup for the list of keys that are weakly tracked too
277
- enumerator . Value . Cleanup ( ) ;
278
- #endif
279
274
}
280
275
281
276
// Remove all the mappings with no handlers left
@@ -363,29 +358,20 @@ public bool Remove(TKey key)
363
358
364
359
/// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
365
360
public IEnumerator < KeyValuePair < TKey , TValue > > GetEnumerator ( )
366
- {
367
- foreach ( WeakReference < TKey > node in this . keys )
368
- {
369
- if ( node . TryGetTarget ( out TKey ? target ) &&
370
- this . table . TryGetValue ( target , out TValue value ) )
371
- {
372
- yield return new KeyValuePair < TKey , TValue > ( target , value ) ;
373
- }
374
- }
375
- }
376
-
377
- /// <summary>
378
- /// Trims the list of tracked <typeparamref name="TKey"/> instances.
379
- /// </summary>
380
- public void Cleanup ( )
381
361
{
382
362
for ( LinkedListNode < WeakReference < TKey > > ? node = this . keys . First ; ! ( node is null ) ; )
383
363
{
384
364
LinkedListNode < WeakReference < TKey > > ? next = node . Next ;
385
365
386
- // Remove all the nodes with a target that has been collected
387
- if ( ! node . Value . TryGetTarget ( out _ ) )
366
+ // Get the key and value for the current node
367
+ if ( node . Value . TryGetTarget ( out TKey ? target ) &&
368
+ this . table . TryGetValue ( target ! , out TValue value ) )
369
+ {
370
+ yield return new KeyValuePair < TKey , TValue > ( target , value ) ;
371
+ }
372
+ else
388
373
{
374
+ // If the current key has been collected, trim the list
389
375
this . keys . Remove ( node ) ;
390
376
}
391
377
0 commit comments