Mono Class Library: System.Array Overview | Members

System.Array.Copy Method

Copies the specified number of elements from the specified source array to the specified destination array. [Edit]

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

Parameters

sourceArray
A Array that contains the data to copy. [Edit]
destinationArray
A Array that receives the data. [Edit]
length
A int designating the number of elements to copy, starting with the first element and proceeding in order. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionsourceArray or destinationArray is null. [Edit]
RankExceptionsourceArray and destinationArray have different ranks. [Edit]
ArrayTypeMismatchException

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.

-or-

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.

-or-

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

[Edit]
InvalidCastException At least one of the elements in sourceArray is not assignment-compatible with the type of destinationArray. [Edit]
ArgumentOutOfRangeExceptionlength < 0. [Edit]
ArgumentException

length > sourceArray.Length.

-or-

length > destinationArray.Length.

[Edit]

Remarks

This version of Array.Copy(Array, Array, int) is equivalent to Array.Copy(Array, Array, int) (sourceArray, sourceArray.GetLowerBound(0), destinationArray, destinationArray.GetLowerBound(0), length).

If sourceArray and destinationArray are of different types, Array.Copy(Array, Array, int) performs widening conversions on the elements of sourceArray as necessary before storing the information in destinationArray. Value types will be boxed when being converted to a object. If the necessary conversion is a narrowing conversion, a ArrayTypeMismatchException exception is thrown.

Note: For information regarding valid conversions performed by this method, see Convert.

If an exception is thrown while copying, the state of destinationArray is undefined.

If sourceArray and destinationArray are the same array, Array.Copy(Array, Array, int) copies the source elements safely to their destination, as if the copy were done through an intermediate array.

[Edit]

Example

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.WriteLine();
      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

Requirements

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