tags:

views:

1687

answers:

2

How can i copy a part of an array to another array.

Consider i'm having, int[] a = {1,2,3,4,5}; now if i give the start index and end index of the array 'a' it should get copied to another array.

Like if i give start index as 1 and end index as 3, the elements 2,3,4 should get copied in new array.!

+17  A: 
int[] b = new int[3];
Array.Copy(a, 1, b, 0, 3);
  • a = source array
  • 1 = start index in source array
  • b = destination array
  • 0 = start index in destination array
  • 3 = elements to copy
Marc Gravell
+2  A: 

See this question. LINQ Take() and Skip() are the most popular answers, as well as Array.CopyTo().

A purportedly faster extension method is described here.

Pontus Gagge