1 Array.clone method
namespaces: System
Assembly: mscorlib
Syntax:
Public Object Clone ()
a shallow copy of an array copies only the elements of an array, whether they are reference types or value types, but is not responsible for the objects referenced by those references.
The reference in the new array points to the same object as the reference to the original array.
Example:
int[] IntArray1 = {1, 2};
int [] intArray2 = (int []) intarray1.clone ();
It is necessary to note that the coercion type conversion is required because the type returned by Clone () is Object
2 Array.copy Method
namespaces: System
Assembly: mscorlib
copy has several overloaded functions:
//Start by copying a series of elements in an array from the first element, pasting them into another array (starting with the first element). Length is a 32-bit integer
Public static void Copy (array sourcearray, array destinationarray, int length)
//Start by copying a series of elements in an array from the first element, pasting them into another array (starting with the first element). Length is a 64-bit integer
Public static void Copy (array sourcearray, array destinationarray, long length)
//start with the specified source index, copy a series of elements from the array, paste them into another array (starting at the specified target index). Length and index specified as 32-bit integers
Public static void Copy (Array sourcearray, int sourceindex,array destinationarray, int destinationindex,int length) /c3>
//start with the specified source index, copy a series of elements from the array, paste them into another array (starting at the specified target index). Length and index specified as 64-bit integers
Public static void Copy (Array sourcearray, Long Sourceindex,array destinationarray, long Destinationindex,long Length)
Example: Array myintarray=array.createinstance (typeof (System.Int32), 5);
for (int i = myintarray.getlowerbound (0); I <= myintarray.getupperbound (0); i++)
{myintarray.setvalue (i+1, i);}
Array Myobjarray = Array.CreateInstance (typeof (System.Object), 5);
for (int i = myobjarray.getlowerbound (0); I <= myobjarray.getupperbound (0); i++)
{myobjarray.setvalue (i+26, i);}
//Copies The first element from the Int32 array to the Object array.
array.copy (Myintarray, Myintarray.getlowerbound (0), Myobjarray, Myobjarray.getlowerbound (0), 1);
//Copies The last of the elements from the Object array to the INT32 array.
array.copy (Myobjarray, Myobjarray.getupperbound (0)-1, Myintarray, Myintarray.getupperbound (0)-1, 2);
difference:
Clone () return value is object,copy return value is void
Clone () is a non-static method, and copy is a static method.
Clone () creates a new array, and the copy method must pass an existing array with the same order and sufficient elements.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The difference between the shallow copy clone () and copy () of the C # array class