How to copy part of an array to another array in C#?
C#ArraysC# Problem Overview
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 the new array.
C# Solutions
Solution 1 - C#
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
Solution 2 - C#
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.
Solution 3 - C#
int[] a = {1,2,3,4,5};
int [] b= new int[a.length]; //New Array and the size of a which is 4
Array.Copy(a,b,a.length);
Where Array is class having method Copy, which copies the element of a array to b array.
While copying from one array to another array, you have to provide same data type to another array of which you are copying.
Solution 4 - C#
Note: I found this question looking for one of the steps in the answer to how to resize an existing array.
So I thought I would add that information here, in case anyone else was searching for how to do a ranged copy as a partial answer to the question of resizing an array.
For anyone else finding this question looking for the same thing I was, it is very simple:
Array.Resize<T>(ref arrayVariable, newSize);
where T is the type, i.e. where arrayVariable is declared:
T[] arrayVariable;
That method handles null checks, as well as newSize==oldSize having no effect, and of course silently handles the case where one of the arrays is longer than the other.
See the MSDN article for more.
Solution 5 - C#
In case if you want to implement your own Array.Copy method.
Static method which is of generic type.
static void MyCopy<T>(T[] sourceArray, long sourceIndex, T[] destinationArray, long destinationIndex, long copyNoOfElements)
{
long totaltraversal = sourceIndex + copyNoOfElements;
long sourceArrayLength = sourceArray.Length;
//to check all array's length and its indices properties before copying
CheckBoundaries(sourceArray, sourceIndex, destinationArray, copyNoOfElements, sourceArrayLength);
for (long i = sourceIndex; i < totaltraversal; i++)
{
destinationArray[destinationIndex++] = sourceArray[i];
}
}
Boundary method implementation.
private static void CheckBoundaries<T>(T[] sourceArray, long sourceIndex, T[] destinationArray, long copyNoOfElements, long sourceArrayLength)
{
if (sourceIndex >= sourceArray.Length)
{
throw new IndexOutOfRangeException();
}
if (copyNoOfElements > sourceArrayLength)
{
throw new IndexOutOfRangeException();
}
if (destinationArray.Length < copyNoOfElements)
{
throw new IndexOutOfRangeException();
}
}
Solution 6 - C#
In C# 8+ you can use ranges.
int a[] = {1,2,3,4,5};
int b[] = a[1..4]; // b = [2,3,4];
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges