Array Clone bytes 10-40?
Is there a wa开发者_如何学Goy i can do
var splice = byteArray.Clone(offset, length);
You can copy the bytes,
byte[] splice = new byte[length];
Array.Copy(byteArray,offset,splice,0,length);
If it is an option to use Linq:
var splice = byteArray.Skip(offset).Take(length).ToArray();
Obligatory LINQ solution:
var splice = byteArray.Skip(offset)
.Take(length)
.ToArray();
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.)
精彩评论