Skip to content

Commit 0a727a5

Browse files
authored
Avoid wrapping in SortedDictionary enumeration as enumerable (#117357)
SortedDictionary's struct enumerator wraps an underlying TreeSet's enumerator. But when the SortedDictionary is enumerated via is `IEnumerable<T>` implementation, rather than boxing and returning the SortedDictionary's enumerator, we can skip that layer and instead directly use the TreeSet's.
1 parent 83e4e28 commit 0a727a5

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/libraries/System.Collections/src/System/Collections/Generic/SortedDictionary.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
245245

246246
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() =>
247247
Count == 0 ? EnumerableHelpers.GetEmptyEnumerator<KeyValuePair<TKey, TValue>>() :
248-
GetEnumerator();
248+
_set.GetEnumerator();
249249

250250
public bool Remove(TKey key)
251251
{
@@ -465,10 +465,8 @@ void IEnumerator.Reset()
465465
{
466466
return new DictionaryEntry(Current.Key, Current.Value);
467467
}
468-
else
469-
{
470-
return new KeyValuePair<TKey, TValue>(Current.Key, Current.Value);
471-
}
468+
469+
return Current;
472470
}
473471
}
474472

0 commit comments

Comments
 (0)