ใช้วิธี Buffer.BlockCopy เพื่อคัดลอกช่วงของไบต์จากอาร์เรย์หนึ่งไปยังอีกอาร์เรย์หนึ่ง -
ตั้งค่าอาร์เรย์ไบต์ −
byte[] b1 = new byte[] {22, 49};
byte[] b2 = new byte[5]; คัดลอกไบต์จากอาร์เรย์หนึ่งไปยังอีกอาร์เรย์หนึ่ง -
Buffer.BlockCopy(b1, 0, b2, 0, 2);
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
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();
}
} ผลลัพธ์
2249000