From eb5a207ccf1173b42ca2f170b2a4acd446d2a819 Mon Sep 17 00:00:00 2001 From: Andrew Hoefling Date: Mon, 4 Mar 2019 22:28:05 -0500 Subject: [PATCH] Added ReplaceItemsRange API and updated ReplaceRange to invoke the protected method Commit migrated from https://github.com/dotnet/coreclr/commit/96de98c1aab0b65c0dfd667bc86e6ab14794306c --- .../src/System/Collections/ObjectModel/Collection.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs index 4d4825a..69d50fb 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs @@ -133,11 +133,7 @@ namespace System.Collections.ObjectModel public void RemoveRange(int index, int count) => RemoveItemsRange(index, count); - public void ReplaceRange(int index, int count, IEnumerable collection) - { - RemoveItemsRange(index, count); - InsertItemsRange(index, collection); - } + public void ReplaceRange(int index, int count, IEnumerable collection) => ReplaceItemsRange(index, count, collection); public void RemoveAt(int index) { @@ -211,12 +207,20 @@ namespace System.Collections.ObjectModel ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); } - for (int i = index; i < (index + count); i++) + int indexCount = index + count; + int length = indexCount > items.Count ? items.Count : indexCount; + for (int i = index; i < length; i++) { RemoveAt(index); } } + protected virtual void ReplaceItemsRange(int index, int count, IEnumerable collection) + { + RemoveItemsRange(index, count); + InsertItemsRange(index, collection); + } + bool ICollection.IsReadOnly { get -- 2.7.4