System.Array.CopyTo Method

Copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index. The index is specified as a 32-bit integer.

Syntax

public void CopyTo (Array array, int index)

Parameters

array
The one-dimensional array that is the destination of the elements copied from the current array.
index
A 32-bit integer that represents the index in array at which copying begins.

Exceptions

TypeReason
ArgumentNullException array is null.
RankExceptionThe current instance has more than one dimension.
ArgumentOutOfRangeException index < array.GetLowerBound(0).
ArgumentException

array has more than one dimension.

-or-

( index + Length of the current instance) > (array.GetLowerBound(0) + array.Length).

-or-

The number of elements in the current instance is greater than the available space from index to the end of array.

ArrayTypeMismatchExceptionThe element type of the current instance is not assignment-compatible with the element type of array.

Remarks

This method copies all the elements of the current array instance to the array destination array, starting at index index. The array destination array must already have been dimensioned and must have a sufficient number of elements to accommodate the copied elements. Otherwise, the method throws an exception.

This method supports the ICollection interface. If implementing ICollection is not explicitly required, use Array.Copy(Array, Array, int) to avoid an extra indirection.

If this method throws an exception while copying, the state of array is undefined.

This method is an O(n) operation, where n is Array.Length. It performs a shallow copy only.

Example

The following example shows how to copy the elements of one Array into another.

C# Example

using System;

public class ArrayCopyToExample
{
   public static void Main()
   {
      Array aryOne = Array.CreateInstance(typeof(Object), 3);
      aryOne.SetValue("one", 0);
      aryOne.SetValue("two", 1);
      aryOne.SetValue("three", 2);

      Array aryTwo = Array.CreateInstance(typeof(Object), 5);
      for (int i=0; i < aryTwo.Length; i++)
         aryTwo.SetValue(i, i);

      Console.WriteLine("The contents of the first array are:");
      foreach (object o in aryOne)
         Console.Write("{0} ", o);
      Console.WriteLine();
      Console.WriteLine("The original contents of the second array are:");
      foreach (object o in aryTwo)
         Console.Write("{0} ", o);
      Console.WriteLine();
      
      aryOne.CopyTo(aryTwo, 1);

      Console.WriteLine("The new contents of the second array are:");
      foreach( object o in aryTwo)
         Console.Write("{0} ", o);
   }
}

The output is

The contents of the first array are:

one two three

The original contents of the second array are:

0 1 2 3 4

The new contents of the second array are:

0 one two three 4

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0