緩衝區 SetByte C# 示例


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

更新於: 22-06-2020

206 次瀏覽

開啟你的 職業

完成課程即可獲得認證

開始
廣告