diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index 34c0e004a99b..482215723c4f 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -127,6 +127,8 @@ public int Count } } + private static void ThrowIndexOutOfRangeException() => throw new IndexOutOfRangeException(); + /// /// Gets or sets the element at the specified index. /// @@ -140,7 +142,7 @@ public T this[int index] { if (index >= this.Count) { - throw new IndexOutOfRangeException(); + ThrowIndexOutOfRangeException(); } return _elements[index]; @@ -150,7 +152,7 @@ public T this[int index] { if (index >= this.Count) { - throw new IndexOutOfRangeException(); + ThrowIndexOutOfRangeException(); } _elements[index] = value; @@ -169,7 +171,7 @@ public ref readonly T ItemRef(int index) { if (index >= this.Count) { - throw new IndexOutOfRangeException(); + ThrowIndexOutOfRangeException(); } return ref this._elements[index]; @@ -247,8 +249,10 @@ public void Insert(int index, T item) /// The object to add to the . public void Add(T item) { - this.EnsureCapacity(this.Count + 1); - _elements[_count++] = item; + int newCount = _count + 1; + this.EnsureCapacity(newCount); + _elements[_count] = item; + _count = newCount; } ///