diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs index ef0381384f29ac..ac8efc506b439a 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs @@ -196,15 +196,20 @@ internal ref readonly T ItemRef(int index) { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + return ref ItemRefUnchecked(index); + } + + private ref readonly T ItemRefUnchecked(int index) + { Debug.Assert(_left != null && _right != null); if (index < _left._count) { - return ref _left.ItemRef(index); + return ref _left.ItemRefUnchecked(index); } if (index > _left._count) { - return ref _right.ItemRef(index - _left._count - 1); + return ref _right.ItemRefUnchecked(index - _left._count - 1); } return ref _key; diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs index 89b044c0ce9793..44d7f1768c6785 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs @@ -261,16 +261,22 @@ internal T this[int index] internal ref readonly T ItemRef(int index) { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + + return ref ItemRefUnchecked(index); + } + + private ref readonly T ItemRefUnchecked(int index) + { Debug.Assert(_left != null && _right != null); if (index < _left._count) { - return ref _left.ItemRef(index); + return ref _left.ItemRefUnchecked(index); } if (index > _left._count) { - return ref _right.ItemRef(index - _left._count - 1); + return ref _right.ItemRefUnchecked(index - _left._count - 1); } return ref _key;