If you find yourself doing this in many places: write an assistant.
public static class ArrayExtensions {
public static Array ClonePart(this Array input, int offset, int length) {
if (input == null) throw new ArgumentNullException("input");
if (offset <= 0 || offset > input.Length) throw new ArgumentOutOfRangeException("offset");
if (length <= 0 || length > input.Length || length+offset > input.Length)
throw new ArgumentOutOfRangeException("length");
var output = Array.CreateInstance(input.GetType().GetElementType(), length);
Array.Copy(input, offset, output, 0, length);
return output;
}
}
(This will work for any type of one-dimensional array.)
source
share