C # several methods for copying Arrays
I was suddenly touched, so I wrote about sharing.
First of all, the array is of the reference type, so be careful not to copy the address while copying, without copying the value!
In fact, when copying arrays, you must use new to open up a new space in the heap for storing arrays. This is effective.
(1)
Int [] pins = {9, 3, 7, 2 };
Int [] copy = new int [pins. length];
For (int I = 0; I <copy. length; I ++)
{
Copy [I] = pins [I];
}
(2)
Int [] copy = new int [pins. Length];
Pins. CopyTo (copy, 0 );
(3)
Int [] pins = new int [4] {9, 3, 7, 2 };
Int [] alias = pins;
Note that this type of replication is only a reference. It only transmits the data address to the alias array. Therefore, this method is not recommended to copy the array;
(4)
Array. Copy (pins, copy, copy. Length)
(5)
Int [] copy = (int []) pins. Clone ();
The reason why int [] is used for forced type conversion is that the Clone result type is object, so it needs to be forcibly converted to int [].
The Object class is actually the base class of all our classes.