Mono Class Library: System.Array Overview | Members

System.Array.Copy Method

Copies the specified number of elements from a source array starting at the specified source index to a destination array starting at the specified destination index. [Edit]

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

Parameters

sourceArray
The Array that contains the data to copy. [Edit]
sourceIndex
A int that contains the index in sourceArray from which copying begins. [Edit]
destinationArray
The Array that receives the data. [Edit]
destinationIndex
A int that contains the index in destinationArray at which storing begins. [Edit]
length
A int that contains the number of elements to copy. [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]
InvalidCastExceptionAt least one element in sourceArray is assignment-incompatible with the type of destinationArray. [Edit]
ArgumentOutOfRangeException

sourceIndex < sourceArray.GetLowerBound(0).

-or-

destinationIndex < destinationArray.GetLowerBound(0).

-or-

length < 0.

[Edit]
ArgumentException

(sourceIndex + length ) > (sourceArray.GetLowerBound(0) + sourceArray.Length).

(destinationIndex + length ) > ( destinationArray.GetLowerBound(0) + destinationArray.Length).

[Edit]

Remarks

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;
class ArrayCopyExample {
   public static void Main() {
      int[] intAry = { 0, 10, 20, 30, 40, 50 };
      Console.Write( "The elements of the array are: " );
      foreach ( int i in intAry )
         Console.Write( "{0,3}", i );
      Console.WriteLine();
      Array.Copy( intAry, 2, intAry, 0, 4 );
      Console.WriteLine( "After copying elements 2 through 5 into elements 0 through 4" );
      Console.Write( "The elements of the array are: " );
      foreach ( int i in intAry )
         Console.Write( "{0,3}", i );
      Console.WriteLine();     
   }
}
   

The output is

The elements of the array are: 0 10 20 30 40 50
After copying elements 2 through 5 into elements 0 through 4
The elements of the array are: 20 30 40 50 40 50

Requirements

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