Added ReplaceItemsRange API and updated ReplaceRange to invoke the protected method
authorAndrew Hoefling <andrew@hoeflingsoftware.com>
Tue, 5 Mar 2019 03:28:05 +0000 (22:28 -0500)
committerSantiago Fernandez Madero <safern@microsoft.com>
Fri, 8 Mar 2019 19:20:58 +0000 (11:20 -0800)
Commit migrated from https://github.com/dotnet/coreclr/commit/96de98c1aab0b65c0dfd667bc86e6ab14794306c

src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs

index 4d4825a..69d50fb 100644 (file)
@@ -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<T> collection)
-        {
-            RemoveItemsRange(index, count);
-            InsertItemsRange(index, collection);
-        }
+        public void ReplaceRange(int index, int count, IEnumerable<T> 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<T> collection)
+        {
+            RemoveItemsRange(index, count);
+            InsertItemsRange(index, collection);
+        }
+
         bool ICollection<T>.IsReadOnly
         {
             get