System.Array.Copy Method

Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element. The length is specified as a 32-bit integer.


[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.MayCorruptInstance, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static void Copy (Array sourceArray, Array destinationArray, int length)


The Array that contains the data to copy.
The Array that receives the data.
A 32-bit integer that represents the number of elements to copy.


ArgumentNullException sourceArray or destinationArray is null.
RankException sourceArray and destinationArray have different ranks.

The elements in both arrays are built-in types, and converting from the type of the elements of sourceArray into the type of the elements in destinationArray requires a narrowing conversion.


Both arrays are built-in types, and one array is a value-type array and the other an array of interface type not implemented by that value-type.


Both arrays are user-defined value types and are not of the same type.

InvalidCastException At least one of the elements in sourceArray is not assignment-compatible with the type of destinationArray.
ArgumentOutOfRangeException length < 0.

length > sourceArray.Length.


length > destinationArray.Length.


The sourceArray and destinationArray parameters must have the same number of dimensions. In addition, destinationArray must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).

If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.


This method is equivalent to the standard C/C++ function memmove, not memcpy.

The arrays can be reference-type arrays or value-type arrays. Type downcasting is performed, as required.

  • When copying from a reference-type array to a value-type array, each element is unboxed and then copied. When copying from a value-type array to a reference-type array, each element is boxed and then copied.

  • When copying from a reference-type or value-type array to an object array, an object is created to hold each value or reference and then copied. When copying from an object array to a reference-type or value-type array and the assignment is not possible, an InvalidCastException is thrown.

  • If sourceArray and destinationArray are both reference-type arrays or are both arrays of type object, a shallow copy is performed. A shallow copy of an Array is a new Array containing references to the same elements as the original Array. The elements themselves or anything referenced by the elements are not copied. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

An ArrayTypeMismatchException is thrown if the arrays are of incompatible types. Type compatibility is defined as follows:

  • A type is compatible with itself.

  • A value type is compatible with object and with an interface type implemented by that value type. A value type is considered connected to an interface only if it implements that interface directly. Disconnected types are not compatible.

  • Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. A widening conversion never loses information, whereas a narrowing conversion can lose information. For example, converting a 32-bit signed integer to a 64-bit signed integer is a widening conversion, and converting a 64-bit signed integer to a 32-bit signed integer is a narrowing conversion. For more information about conversions, see Convert.

  • A nonintrinsic (user-defined) value type is compatible only with itself.

  • Enumerations have an implicit conversion to Enum and to their underlying type.

If every element in sourceArray requires a downcast (for example, from a base class to a derived class or from an interface to an object) and one or more elements cannot be cast to the corresponding type in destinationArray, an InvalidCastException is thrown.

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

This method is an O(n) operation, where n is length.


This example demonstrates the Array.Copy(Array, Array, int) method.

C# Example

using System;
public class ArrayCopyExample {
   public static void Main() {
      int[] intAryOrig = new int[3];
      double[] dAryCopy = new double[3];
      for ( int i = 0; i < intAryOrig.Length; i++ )
         intAryOrig[i] = i+3;
      //copy the first 2 elements of the source into the destination
      Array.Copy( intAryOrig, dAryCopy, 2);
      Console.Write( "The elements of the first array are: " );
      for ( int i = 0; i < intAryOrig.Length; i++ ) 
         Console.Write( "{0,3}", intAryOrig[i] );
      Console.Write( "The elements of the copied array are: " );
      for ( int i = 0; i < dAryCopy.Length; i++ ) 
         Console.Write( "{0,3}", dAryCopy[i] );

The output is

The elements of the first array are: 3 4 5
The elements of the copied array are: 3 4 0


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