C# ArrayList CopyTo(Array)

Description

ArrayList CopyTo(Array) copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.

Syntax

ArrayList.CopyTo(Array) has the following syntax.


public virtual void CopyTo(
  Array array
)

Parameters

ArrayList.CopyTo(Array) has the following parameters.

  • array - The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.

Returns

ArrayList.CopyTo(Array) method returns

Example

The following code example shows how to copy an ArrayList into a one-dimensional System.Array.


//ww w .  java  2  s  . c om
using System;
using System.Collections;
public class SamplesArrayList  {

    public static void Main()  {
       ArrayList mySourceList = new ArrayList();
       mySourceList.Add( "A" );
       mySourceList.Add( "B" );
       mySourceList.Add( "C" );
       mySourceList.Add( "D" );
       mySourceList.Add( "E" );
       mySourceList.Add( "F" );

       String[] myTargetArray = new String[15];
       myTargetArray[0] = "0";
       myTargetArray[1] = "1";
       myTargetArray[2] = "2";
       myTargetArray[3] = "3";
       myTargetArray[4] = "4";
       myTargetArray[5] = "5";
       myTargetArray[6] = "6";
       myTargetArray[7] = "7";
       myTargetArray[8] = "8";

       // Copies the second element from the source ArrayList to the target Array starting at index 7.
       mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
       // Copies the entire source ArrayList to the target Array starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );

       // Copies the entire source ArrayList to the target Array starting at index 0.
       mySourceList.CopyTo( myTargetArray );

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack