Edit

Share via


List<T>.AddRange(IEnumerable<T>) Method

Definition

Adds the elements of the specified collection to the end of the List<T>.

public:
 void AddRange(System::Collections::Generic::IEnumerable<T> ^ collection);
public void AddRange(System.Collections.Generic.IEnumerable<T> collection);
member this.AddRange : seq<'T> -> unit
Public Sub AddRange (collection As IEnumerable(Of T))

Parameters

collection
IEnumerable<T>

The collection whose elements should be added to the end of the List<T>. The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.

Exceptions

collection is null.

Examples

The following example demonstrates the AddRange method and various other methods of the List<T> class that act on ranges. An array of strings is created and passed to the constructor, populating the list with the elements of the array. The AddRange method is called, with the list as its argument. The result is that the current elements of the list are added to the end of the list, duplicating all the elements.

using System;
using System.Collections.Generic;

string[] input = { "Apple",
                   "Banana",
                   "Orange" };

List<string> fruits = new List<string>(input);

Console.WriteLine("\nCapacity: {0}", fruits.Capacity);
Console.WriteLine();

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

Console.WriteLine("\nAddRange(fruits)");
fruits.AddRange(fruits);

Console.WriteLine();
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

Console.WriteLine("\nRemoveRange(2, 2)");
fruits.RemoveRange(2, 2);

Console.WriteLine();
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

input = new string[] { "Mango",
                       "Pineapple",
                       "Watermelon" };

Console.WriteLine("\nInsertRange(3, input)");
fruits.InsertRange(3, input);

Console.WriteLine();
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()");
string[] output = fruits.GetRange(2, 3).ToArray();

Console.WriteLine();
foreach (string fruit in output)
{
    Console.WriteLine(fruit);
}

/*
    This code example produces the following output:

    Capacity: 3

    Apple
    Banana
    Orange

    AddRange(fruits)

    Apple
    Banana
    Orange
    Apple
    Banana
    Orange

    RemoveRange(2, 2)

    Apple
    Banana
    Banana
    Orange

    InsertRange(3, input)

    Apple
    Banana
    Banana
    Mango
    Pineapple
    Watermelon
    Orange

    output = fruits.GetRange(2, 3).ToArray()

    Banana
    Mango
    Pineapple
*/
Imports System.Collections.Generic

Partial Public Class Program
    Public Shared Sub ShowFruits()

        Dim input() As String = { "Apple", _
                                  "Banana", _
                                  "Orange" }

        Dim fruits As New List(Of String)(input)

        Console.WriteLine(vbLf & "Capacity: {0}", fruits.Capacity)
        Console.WriteLine()

        For Each fruit As String In fruits
            Console.WriteLine(fruit)
        Next

        Console.WriteLine(vbLf & "AddRange(fruits)")
        fruits.AddRange(fruits)

        Console.WriteLine()
        For Each fruit As String In fruits
            Console.WriteLine(fruit)
        Next

        Console.WriteLine(vbLf & "RemoveRange(2, 2)")
        fruits.RemoveRange(2, 2)

        Console.WriteLine()
        For Each fruit As String In fruits
            Console.WriteLine(fruit)
        Next

        input = New String() { "Mango", _
                               "Pineapple", _
                               "Watermelon" }

        Console.WriteLine(vbLf & "InsertRange(3, input)")
        fruits.InsertRange(3, input)

        Console.WriteLine()
        For Each fruit As String In fruits
            Console.WriteLine(fruit)
        Next

        Console.WriteLine(vbLf & "output = fruits.GetRange(2, 3).ToArray")
        Dim output() As String = fruits.GetRange(2, 3).ToArray()

        Console.WriteLine()
        For Each fruit As String In output
            Console.WriteLine(fruit)
        Next

    End Sub
End Class

' This code example produces the following output:
'
' Capacity: 3
'
' Apple
' Banana
' Orange
'
' AddRange(fruits)
'
' Apple
' Banana
' Orange
' Apple
' Banana
' Orange
'
' RemoveRange(2, 2)
'
' Apple
' Banana
' Banana
' Orange
'
' InsertRange(3, input)
'
' Apple
' Banana
' Banana
' Mango
' Pineapple
' Watermelon
' Orange
'
' output = fruits.GetRange(2, 3).ToArray
'
' Banana
' Mango
' Pineapple

Remarks

The order of the elements in the collection is preserved in the List<T>.

If the new Count (the current Count plus the size of the collection) will be greater than Capacity, the capacity of the List<T> is increased by automatically reallocating the internal array to accommodate the new elements, and the existing elements are copied to the new array before the new elements are added.

If the List<T> can accommodate the new elements without increasing the Capacity, this method is an O(n) operation, where n is the number of elements to be added. If the capacity needs to be increased to accommodate the new elements, this method becomes an O(n + m) operation, where n is the number of elements to be added and m is Count.

Applies to

See also