In C#, List.InsertRange(index, collection) puts a whole group of items into a list at a position. The items of the collection keep their own order, and the list items that were at the position and after move to the right.
It is the combination of Insert() and AddRange(). The index rule is the same as for Insert: valid positions are 0 to Count. With index Count it works exactly like AddRange.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.InsertRange(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass the position and the collection to insert. The first item of the collection ends up at that position.
Inserts 3 then 4 starting at index 2. Later items shift right by 2.
numbers.InsertRange(0, other)
Puts a copy of every item in other at the front of the list.
numbers.InsertRange(numbers.Count, other)
Index Count is allowed and appends, like AddRange.
A bad index (below 0 or above Count) throws ArgumentOutOfRangeException, and a null collection throws ArgumentNullException. Inserting an empty collection changes nothing. The items after the position are moved only once, which is faster than calling Insert in a loop.
Library declarations
C#
public void InsertRange(int index, IEnumerable<T> collection);
Parameters
Parameter
Description
index
Required. The zero-based position where the first new item will be placed. It must be from 0 to Count.
collection
Required. The items to insert. It can be an array, a List<T>, or any IEnumerable<T>. It must not be null.
Return Value
Return Value
Description
void
Nothing is returned. The list changes in place and Count grows by the number of inserted items.
C# Examples
Example 1 — Insert Numbers into the Middle
The list is missing 3, 4, and 5. Insert them from an array in one call.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 6 };
numbers.InsertRange(2, new int[] { 3, 4, 5 });
Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(numbers.Count);
Console.WriteLine(numbers.IndexOf(6));
}
}
Output
1, 2, 3, 4, 5, 6
6
5
How It Works
Gap at index 2. The list holds 1, 2, 6. Index 2 is where 6 lives, so that is where the new items go.
One move.6 and anything after it shift right by three places, and 3, 4, 5 fill the space in the given order.
Count adds up. Three old items plus three new items make a Count of 6.
Index follows.6 was at index 2 and is now at index 5, the last position.
Example 2 — Insert at Index 0 and at Count
Add a group at the front, then add another group at the end by using Count as the index.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int> { 3, 4 };
list.InsertRange(0, new int[] { 1, 2 });
Console.WriteLine(string.Join(", ", list));
list.InsertRange(list.Count, new int[] { 5, 6 });
Console.WriteLine(string.Join(", ", list));
}
}
Output
1, 2, 3, 4
1, 2, 3, 4, 5, 6
How It Works
Front insert.InsertRange(0, ...) places 1, 2 before everything else, and the old items move two places right.
End insert.list.Count is 4, which is allowed as an index. The items 5, 6 land after the last item.
Same as AddRange. Using Count as the index gives the same result as AddRange. Only the position makes it different.
Group order kept. In both calls the inserted numbers stay in ascending order, exactly as they are in the array.
Example 3 — Insert a List and Handle a Bad Index
Insert one list into another, then try an index that is too big and catch the exception.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> main = new List<string> { "start", "end" };
List<string> extra = new List<string> { "one", "two" };
main.InsertRange(1, extra);
Console.WriteLine(string.Join(", ", main));
Console.WriteLine(extra.Count);
try {
main.InsertRange(10, extra);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("bad index");
}
Console.WriteLine(main.Count);
}
}
Output
start, one, two, end
2
bad index
4
How It Works
List into list.extra is read item by item and placed between "start" and "end". Its own order, one, two, is kept.
Source untouched.extra.Count is still 2, because InsertRange only reads from the collection.
Too big an index. The list has 4 items, so index 10 is not allowed. ArgumentOutOfRangeException is thrown and caught here.
List unchanged after failure. The failed call inserted nothing, so Count is still 4.
Frequently Asked Questions
It inserts all items of a collection into the list at the index you give, keeping their order, and moves the later list items to the right.
AddRange always adds at the end. InsertRange adds at any index from 0 to Count. InsertRange(list.Count, items) behaves like AddRange.
No. They keep the order they have in the source collection.
ArgumentOutOfRangeException when the index is below 0 or above Count, and ArgumentNullException when the collection is null.
Yes. InsertRange shifts the later items once, while a loop of Insert calls shifts them again for every item.