SetByte() วิธีการกำหนดค่าที่ระบุให้กับไบต์ที่ตำแหน่งเฉพาะในอาร์เรย์ที่ระบุ
ขั้นแรก ตั้งค่าอาร์เรย์ −
int[] arr = { 3, 4, 12 }; ตอนนี้ ใช้ SetByte() เพื่อกำหนดค่า -
Buffer.SetByte(arr, 3, 20);
นี่คือรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Text;
public class Demo {
public static void Main() {
int[] arr = { 3, 4, 12 };
Console.WriteLine("Initial Array...");
// loop through the byte array
for (int i = 0; i < Buffer.ByteLength(arr); i++) {
Console.WriteLine(Buffer.GetByte(arr, i));
}
Buffer.SetByte(arr, 3, 20);
Console.WriteLine("New Array...");
// loop through the new byte array
for (int i = 0; i < Buffer.ByteLength(arr); i++) {
Console.WriteLine(Buffer.GetByte(arr, i));
}
}
} ผลลัพธ์
Initial Array... 3 0 0 0 4 0 0 0 12 0 0 0 New Array... 3 0 0 20 4 0 0 0 12 0 0 0