Mono Class Library: System.Array Overview | Members

System.Array.CopyTo Method

Copies all the elements of the current zero-based instance to the specified one-dimensional array starting at the specified subscript in the destination array. [Edit]

public void CopyTo (Array array, int index)

Parameters

array
A one-dimensional Array that is the destination of the elements copied from the current instance. [Edit]
index
A int that contains the index in array at which copying begins. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionarray is null. [Edit]
RankExceptionThe current instance has more than one dimension. [Edit]
ArgumentOutOfRangeExceptionindex < array.GetLowerBound(0). [Edit]
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.

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

Remarks

index is the array index in the destination array at which copying begins.

Note:

This method is implemented to support 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.

Operation
As described above.

As described above.

Note to Inheritors
Override this method to copy elements of the current instance to a specified array.

Usage
Use this method to copy elements of the current instance to a specified array.

[Edit]

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