AddRange
, InsertRange
In C#, AddRange
adds an entire collection of elements. It can replace tedious foreach
-loops that repeatedly call Add on List
.
We can pass any IEnumerable
collection to AddRange
, not just an array or another List
. InsertRange()
operates in a similar way, but receives a start position.
AddRange
exampleWe look at the AddRange
instance method from the base class library in the C# language on List
. This example adds a range at the end of the List
using the AddRange
method.
List
is created, and 4 ints are added. An array of 3 elements of the same numeric type is initialized.AddRange
. The 3 values stored in the array are added to the end of the List
.List
itself and the array we added with AddRange
.IEnumerable
collection. Any type that implements IEnumerable
can be used.using System; using System.Collections.Generic; // Step 1: create List and array. var list = new List<int>(); list.Add(1); list.Add(2); list.Add(5); list.Add(6); var array = new int[3]; array[0] = 7; array[1] = 6; array[2] = 7; // Step 2: call AddRange. list.AddRange(array); // Step 3: display the List contents. foreach (int value in list) { Console.WriteLine("VALUE: {0}", value); }VALUE: 1 VALUE: 2 VALUE: 5 VALUE: 6 VALUE: 7 VALUE: 6 VALUE: 7
InsertRange
exampleThis method places a collection of elements into a certain index of a List
. The List
resizes to accommodate the inserted elements.
List
instance with 4 integral values. Next it creates an array with 3 more values.InsertRange
. The program inserts the array into the List
at the second index.List
, which has the contents of the array added at the second index.using System; using System.Collections.Generic; // Step 1: create List and array. var list = new List<int>(); list.Add(1); list.Add(2); list.Add(5); list.Add(6); var array = new int[3]; array[0] = 7; array[1] = 6; array[2] = 7; // Step 2: call InsertRange. list.InsertRange(1, array); // Step 3: display the elements. foreach (int value in list) { Console.WriteLine("VALUE: {0}", value); }VALUE: 1 VALUE: 7 VALUE: 6 VALUE: 7 VALUE: 2 VALUE: 5 VALUE: 6
In .NET, InsertRange
is implemented by using Array.Copy
and the CopyTo
instance method on arrays. The entire array inside the List
will have to be copied.
InsertRange
. If the new range fits in the allocated size, the new range is simply copied.Array.Copy
calls are needed: one for the elements before, and one for the elements after. A new array is allocated.this.EnsureCapacity(this._size + count); if (index < this._size) { Array.Copy(...); } if (this == is2) { Array.Copy(...); Array.Copy(...); } else { T[] array = new T[count]; is2.CopyTo(array, 0); array.CopyTo(...); }
AddRange
The AddRange
method is implemented with InsertRange
. AddRange
is a wrapper method on top of InsertRange
—the target position is 0.
AddRange
and InsertRange
are internally the same method. We know from the compiled code that the internal implementation normally calls Array.Copy
or CopyTo
.
AddRange
or InsertRange
to perform better than a plain Array.Copy
.Array.Copy
itself may perform better than manually copying elements in a loop.AddRange
appends a range of values to the end of a List
. InsertRange
places an array between the elements in a List
. These 2 methods help with List
range manipulations.