Possible Duplicate:
C#: Any faster way of copying arrays?
I have an integer array
int[] a;
I want to assign a copy of it(not reference) to
int[] b;
what is the easier way to do it?
To duplicate an array, just return the element in your map call. numbers = [1, 2, 3]; numbersCopy = numbers. map((x) => x); If you'd like to be a bit more mathematical, (x) => x is called identity.
Use the clone method of the array.
The System ClassarrayCopy(). This copies an array from a source array to a destination array, starting the copy action from the source position to the target position until the specified length. The number of elements copied to the target array equals the specified length.
You can use the native Clone
method, try something like this:
int[] b = (int[])a.Clone();
Other options are, using linq:
using System.Linq;
// code ...
int[] b = a.ToArray();
And copying the array
int[] b = new int[a.Length];
a.CopyTo(b, 0);
I think simplest way would be
int[] b = a.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With