Est-il possible que je peux faireArray Clone octets 10-40?
var splice = byteArray.Clone(offset, length);
Est-il possible que je peux faireArray Clone octets 10-40?
var splice = byteArray.Clone(offset, length);
Vous pouvez copier les octets,
byte[] splice = new byte[length];
Array.Copy(byteArray,offset,splice,0,length);
S'il est une option pour utiliser LINQ:
var splice = byteArray.Skip(offset).Take(length).ToArray();
obligatoire solution LINQ:
var splice = byteArray.Skip(offset)
.Take(length)
.ToArray();
Si vous vous trouvez en train de faire cela dans beaucoup d'endroits: écrivez un 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;
}
}
(Cela fonctionne pour tout type d'un tableau à deux dimensions.)