tags:

views:

64

answers:

4

Is there a way i can do

var splice = byteArray.Clone(offset, length);
+6  A: 

You can copy the bytes,

byte[] splice = new byte[length];
Array.Copy(byteArray,offset,splice,0,length);
nos
+3  A: 

If it is an option to use Linq:

var splice = byteArray.Skip(offset).Take(length).ToArray();
Anders Abel
A: 

Obligatory LINQ solution:

var splice = byteArray.Skip(offset)
                      .Take(length)
                      .ToArray();
Tim Robinson
+1  A: 

If you find yourself doing this in many places: write a helper.

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.)

Richard