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?
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?
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();
You can use Array.CopyTo
method.
int[] a = new[] { 1, 2, 3 };
int[] b = new int[3];
a.CopyTo(b,0);