diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs index f6c47050858cd8..e3ae983bdd7791 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs @@ -96,12 +96,12 @@ public override void Clear() return; } - List toRemove = new List(); + List toRemove = new List(Count); BreadthFirstTreeWalk(n => { toRemove.Add(n.Item); return true; }); - while (toRemove.Count != 0) + + for (int i = toRemove.Count - 1; i >= 0; i--) { - _underlying.Remove(toRemove[^1]); - toRemove.RemoveAt(toRemove.Count - 1); + _underlying.Remove(toRemove[i]); } root = null;