Buffer SetByte Example in C#


The SetByte() method assigns a specified value to a byte at a particular location in a specified array.

Firstly, set an array −

int[] arr = { 3, 4, 12 };

Now, use SetByte() to assign values −

Buffer.SetByte(arr, 3, 20);

Here is the complete code −

Example

 Live Demo

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));
      }
   }
}

Output

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

Updated on: 22-Jun-2020

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements