C# List InsertRange

Description

List InsertRange inserts the elements of a collection into the List at the specified index.

Syntax

List.InsertRange has the following syntax.


public void InsertRange(
  int index,
  IEnumerable<T> collection
)

Parameters

List.InsertRange has the following parameters.

  • index - The zero-based index at which the new elements should be inserted.
  • collection - The collection whose elements should be inserted into the List . The collection itself cannot be null, but it can contain elements that are null, if type T is a reference type.

Example

The following code shows how to add elements a List.


using System;//from www. ja  v a2 s  . c  o m
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        List<string> words = new List<string>();  

        words.InsertRange(0, new[] { "E", "F" });

        
        foreach (string s in words) {
            Console.WriteLine ("all:"+s);   // all words 
        }
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack