Sử dụng phương thức Buffer.BlockCopy để sao chép một dải byte từ mảng này sang mảng khác -
Đặt một mảng byte -
byte[] b1 = new byte[] {22, 49};
byte[] b2 = new byte[5]; Sao chép byte từ mảng này sang mảng khác -
Buffer.BlockCopy(b1, 0, b2, 0, 2);
Sau đây là mã hoàn chỉnh -
Ví dụ
using System;
class Demo {
static void Main(){
// byte arrays
byte[] b1 = new byte[] {22, 49};
byte[] b2 = new byte[5];
// copying bytes from one to another
Buffer.BlockCopy(b1, 0, b2, 0, 2);
/* calling the method with the byte array b2 that has the copied elements */
bufferFunc(b2);
}
static void bufferFunc(byte[] a) {
for (int j = 0; j < a.Length; j++) {
Console.Write(a[j]);
}
Console.WriteLine();
}
} Đầu ra
2249000