C# ArrayList SetRange

Description

ArrayList SetRange copies the elements of a collection over a range of elements in the ArrayList.

Syntax

ArrayList.SetRange has the following syntax.


public virtual void SetRange(
  int index,
  ICollection c
)

Parameters

ArrayList.SetRange has the following parameters.

  • index - The zero-based ArrayList index at which to start copying the elements of c.
  • c - The ICollection whose elements to copy to the ArrayList. The collection itself cannot be null, but it can contain elements that are null.

Returns

ArrayList.SetRange method returns

Example

Use the SetRange() method to copy the elements from anotherStringArray to myArrayList, starting at index 0.


using System;/*  www . j  a v  a 2 s .co m*/
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");

    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);


    DisplayArrayList("myArrayList", myArrayList);
  }
  public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
  {
    for (int i = 0; i < myArrayList.Count; i++){
      Console.WriteLine(arrayListName + "[" + i + "] = " +
        myArrayList[i]);
    }
  }
  
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack