Create a copy of an integer array

Possible duplicate:
C #: Any faster way to copy arrays?

I have an integer array

int[] a;

      

I want to assign a copy of it (not a link) to

int[] b;

      

What's an easier way to do this?

+3


source to share


3 answers


You can use a method Clone

, try something like this:



int[] b = (int[])a.Clone();

      

+11


source


I think the simplest way would be



int[] b = a.ToArray();

      

+3


source


You can use the method Array.CopyTo

.

int[] a = new[] { 1, 2, 3 };
int[] b = new int[3];
a.CopyTo(b,0);

      

+2


source







All Articles