C# ArrayList - CopyTo() Method



The C# ArrayList CopyTo() method is used to copy the elements of an ArrayList into a one-dimensional array.

This method is useful when working with a regular array instead of an ArrayList.

Syntax

Following are the syntax of the C# ArrayList CopyTo() method −

It copies all the items from the ArrayList to the target array, starting from the start of the target array.

public void CopyTo(Array array);

It copies all items from the ArrayList to the specified array, starting at the specified position in the target array.

public void CopyTo(Array array, int index);

It copies a given number of items starting at the specified index in the ArrayList to the specified array starting at the defined position in the target array.

public void CopyTo(int index, Array array, int arrayIndex, int count);

Parameters

This method accepts the following parameters according to all overloaded syntax −

  • array: It is a one-dimensional targeted array where the elements of the ArrayList will be copied.
  • index: It is an index in the targeted array at which copying begins.
  • arrayIndex: It is a zero-based index in the destination array at which storing begins.
  • count: The number of elements to be copied.

Return value

This method does not return any value. It is void type.

Example 1: Using Using CopyTo(Array array)

Following is the basic example of the CopyTo() method to copy items of the ArrayList into a one-dimensional array −

    
using System;
using System.Collections;
class Program
{
   static void Main()
   {
      // Create an ArrayList
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      // Create a target array
      string[] array = new string[arrayList.Count];

      arrayList.CopyTo(array);
      Console.WriteLine("Target Array: " + string.Join(", ", array));
   }
}

Output

Following is the output −

Target Array: A, B, C, D

Example 2: Using CopyTo(Array array, int index)

Let's see another example of the CopyTo method. Here, we copies all items from the ArrayList to the specified array, starting at the specified position in the target array −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      // Create an ArrayList
      ArrayList arrayList = new ArrayList { 1, 2, 3, 4 };

      int[] array = new int[6];

      // Copy elements to the target array starting at index 2
      arrayList.CopyTo(array, 2);
      Console.WriteLine("Target Array: " + string.Join(", ", array));
   }
}

Output

Following is the output −

Target Array: 0, 0, 1, 2, 3, 4

Example 3: Using CopyTo(int index, Array array, int arrayIndex, int count)

This is another version of the CopyTo() method. Here, we copy given number of items starting at the specified index in the ArrayList to the specified array starting at the defined position in the target array −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      // Create an ArrayList
      ArrayList arrayList = new ArrayList { "A", "Y", "M", "W" };
	  
	  // target array
      string[] array = new string[5];

      // Copy 2 elements starting from index 1 in the ArrayList
      // to the target array starting at index 2
      arrayList.CopyTo(1, array, 2, 2);

      Console.WriteLine("Target Array: " + string.Join(", ", array));
   }
}

Output

Following is the output −

Target Array: , , Y, M,

Example 4: Copy Items of ArrayList in 1D-array

The following example uses the CopyTo method to copy the elements of ArrayList to a one-dimensional array. We then display the targeted array using the for-each loop −

using System;
using System.Collections;

class Demo
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "Red", "Blue", "Green", "yellow" };

      // Create a target array with sufficient size to hold the elements
      string[] tArray = new string[arrayList.Count];

      arrayList.CopyTo(tArray);

      Console.WriteLine("Elements in the target array:");
      foreach (string color in tArray)
      {
         Console.WriteLine(color);
      }
   }
}

Output

Following is the output −

Elements in the target array:
Red
Blue
Green
yellow
csharp_arraylist.htm
Advertisements