In C#, List.AddRange(collection) adds many items at once. It walks through the collection you give it and appends each item to the end of the list, in the same order.
The collection can be an array, another List<T>, or any IEnumerable<T>, such as the result of a LINQ query. The source is only read, so it stays unchanged. It is the short way to write a loop of Add() calls.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.AddRange(...). 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 any collection of the same item type. The items are added after the last item that is already in the list.
Appends the two array items at the end of the list.
numbers.AddRange(otherList)
Appends a copy of every item in otherList. otherList is not changed.
numbers.AddRange(Enumerable.Range(1, 3))
Works with any IEnumerable<int>. Needs using System.Linq; for Enumerable.
If the collection is null, AddRange throws ArgumentNullException. Adding an empty collection is fine and changes nothing. The new items are shallow copies: for reference types, the list holds the same objects as the source, not new ones.
Library declarations
C#
public void AddRange(IEnumerable<T> collection);
Parameters
Parameter
Description
collection
Required. The items to add. 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 is changed in place and Count grows by the number of items in the collection.
C# Examples
Example 1 — Append an Array to a List
Start with two numbers and add three more 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 };
int[] more = { 3, 4, 5 };
numbers.AddRange(more);
Console.WriteLine(numbers.Count);
Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(more.Length);
}
}
Output
5
1, 2, 3, 4, 5
3
How It Works
Start with two. The collection initializer creates a list that already holds 1 and 2.
One call, three items.AddRange(more) appends 3, 4, and 5 after the last item, so Count becomes 5.
Order is copied as is. The new items appear in the same order they had in the array.
The array stays.more.Length is still 3, because AddRange copies values out of the array and never edits it.
Example 2 — Merge Two Lists
Add the items of one list to another list. The list you read from is left alone.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> team = new List<string> { "Asha", "Ben" };
List<string> guests = new List<string> { "Chen", "Dia" };
team.AddRange(guests);
Console.WriteLine(string.Join(", ", team));
Console.WriteLine(string.Join(", ", guests));
team.AddRange(guests);
Console.WriteLine(team.Count);
}
}
Output
Asha, Ben, Chen, Dia
Chen, Dia
6
How It Works
Two lists.team has two names and guests has two names. They are separate list objects.
Merge in place.team.AddRange(guests) puts "Chen" and "Dia" after "Ben". Only team changes.
Guests unchanged. The second line shows guests still holds just its own two names.
Repeats allowed. Calling AddRange with the same list again adds the same names a second time. Count grows from 4 to 6, because a List does not remove repeats.
Example 3 — Add a Sequence and Handle null
AddRange accepts any sequence, such as Enumerable.Range. A null collection throws an exception.
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> list = new List<int>();
list.AddRange(Enumerable.Range(1, 3));
list.AddRange(new int[0]);
Console.WriteLine(string.Join(", ", list));
try {
list.AddRange(null);
} catch (ArgumentNullException) {
Console.WriteLine("null is not allowed");
}
Console.WriteLine(list.Count);
}
}
Output
1, 2, 3
null is not allowed
3
How It Works
A sequence works too.Enumerable.Range(1, 3) produces 1, 2, 3 one at a time. AddRange takes any IEnumerable<int>, not only arrays.
Empty is harmless. Adding an empty array changes nothing, so the printed list is still 1, 2, 3.
Null throws. Passing null raises ArgumentNullException. The catch block prints a message, and the list is not touched.
Final count. The list still holds exactly the three numbers from the sequence, so Count is 3.
Frequently Asked Questions
It adds every item of a collection to the end of the list in one call, keeping the order of the source collection.
Any IEnumerable<T> of the same item type: an array, another List<T>, a HashSet<T>, or a LINQ query result.
No. The source is only read. For reference types, the list ends up holding references to the same objects as the source.
It throws ArgumentNullException. An empty collection is allowed and simply adds nothing.
Often yes. When the source knows its size, the list can grow once instead of several times, and it can copy items in bulk.