You don't need to write your own function, the .NET Framework has already everything built in. You have two options:
Array.Copy(sourceArray, srcStartIndex, targetArray, tgtStartIndex, srcNumberOfElements);
sourceArray.CopyTo(targetArray, tgtStartIndex);
I will first explain them with some examples, and at the end I will put it together to a function
public static T[] CopyFromArray<T>(this T[] sourceArray, int startIndex, int endIndex)
as you requested it in your question (CopyTo is already reserved by .NET so I renamed it to CopyFromArray).
Note that sourceArray
and targetArray
are arrays of any type; srcStartIndex
is the start index of the sourceArray
to copy from, tgtStartIndex
is the start index of the targetArray
to copy to, and srcNumberOfElements
is the number of elements you want to copy from the source array.
The target array must be big enough to copy all elements into, otherwise you will get an error.
The elements of the source array are directly copied into the target array ("in situ"), which is why there isn't any data returned from the functions (return type is void
).
Look at this example:
void Main()
{
var target = new int[20];
var srcStart=1; var tgtStart=3; var srcElements=3;
Array.Copy((new int[] { 1,2,3,4,5,6,7,8,9 }), srcStart,
target, tgtStart, srcElements);
Console.WriteLine(string.Join(",", target));
}
It works correctly and returns:
0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Note that srcStart
specifies the index in the source array, from which it starts to copy, while tgtStart
specifies the index in the target array where it starts to insert, and finally, srcElements
specifies how many elements there are to be copied.
Note there is also a simpler version, the .CopyTo
method. It works as follows:
void Main()
{
var target = new int[20]; var tgtStart = 3;
(new int[] { 1,2,3,4,5,6,7,8,9 }).CopyTo(target, tgtStart);
Console.WriteLine(string.Join(",", target));
}
And it returns:
0,0,0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,0
The 2nd parameter specifies the index at which position the data shall be inserted (just like tgtStart
in the previous example), this is why the first 3 elements are 0.
You can use it with other data types too, for example:
var target = new string[20];
(new string[] { "abc", "def", "ghi" }).CopyTo(target, 3);
works in the same way.
Coming to your question, you can use the information I gave you so far to put together your own generic extension method like this:
void Main()
{
int[] newThing1 = (new int[] { 10, 9, 8, 7, 6 }).CopyFromArray(2, 4);
newThing1.Dump();
string[] newThing2 = (new string[] { "A", "B", "C", "D"}).CopyFromArray(1, 3);
newThing2.Dump();
}
public static class Extensions
{
public static T[] CopyFromArray<T>(this T[] sourceArray, int startIndex, int endIndex)
{
int numberOfElements = endIndex - startIndex + 1;
var targetArray = new T[numberOfElements];
Array.Copy(sourceArray, startIndex, targetArray, 0, numberOfElements);
return targetArray;
}
}