Mono Class Library: System.Array Overview | MembersSystem.Array.Copy Method |
Copies the specified number of elements from the specified source array to the specified destination array. [Edit]
|
Type Reason ArgumentNullException sourceArray or destinationArray is null. [Edit] RankException sourceArray 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] ArgumentOutOfRangeException length < 0. [Edit] ArgumentException length > sourceArray.Length.
-or-
length > destinationArray.Length.
[Edit]
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]
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
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0