If I have a simple Utility function that copies an array to a new array:
public static object[] CopyTo(object[] original, int startIndex, int endIndex)
{
List<object> copied - new List<object>();
for (int i = startIndex; i <= endIndex; i++)
{
copied.Add(original[i]);
}
return copied.ToArray();
}
and I want to then be able to call it like this:
int[] newThing = CopyTo(new int[] { 10, 9, 8, 7, 6 }, 2, 4);
the compiler errors saying cannot convert from int[] to object[]
. This is expected since my CopyTo
function specifically wants an object array, not an integer array.
How can I change the declaration of CopyTo in order for it to dynamically accept and return an array of any type? I believe Generics is the way (though I'm not too familiar with this) so I tried:
public static T[] CopyTo(T[] original, int startIndex......)
but the compiler won't recognise T as a type.
To make it generic use following code:
public static T[] CopyTo<T>(T[] original, int startIndex, int endIndex)
{
List<T> copied = new List<T>();
for (int i = startIndex; i <= endIndex; i++)
{
copied.Add(original[i]);
}
return copied.ToArray();
}
Edit:
Just to mention, you can also do this without creating a List<T>
and returning the list as an array. Just create an array (with length equal to the count of wanted elements) and fill it up:
public static T[] CopyTo<T>(T[] original, int startIndex, int endIndex)
{
int count = (endIndex - startIndex) + 1;
int index = 0;
T[] copied = new T[count];
for (int i = startIndex; i <= endIndex; i++)
copied[index++] = original[i];
return copied;
}
And you could also create an extension method for it:
public static class Extensions
{
public static T[] CopyTo<T>(this T[] source, int start, int end)
{
int count = (end - start) + 1;
int index = 0;
T[] copied = new T[count];
for (int i = start; i <= end; i++)
copied[index++] = source[i];
return copied;
}
}
Now you can call it like:
var original = new int[] { 10, 9, 8, 7, 6 };
var newThing = original.CopyTo(0, 2);
Or for an array of strings:
var strOrig = "one.two.three.four.five.six.seven".Split('.');
var strNew = strOrig.CopyTo(2, 5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With