Buffer GetByte Example in C#


Read individual bytes using GetByte() method in C# −

Set an array −

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

Now, use Buffer.GetByte() to display the array elements and to read individual bytes −

for (int i = 0; i < Buffer.ByteLength(arr); i++) {
   Console.WriteLine(Buffer.GetByte(arr, i));
}

The following is the code −

Example

 Live Demo

using System;
using System.Text;
public class Demo {
   public static void Main() {
      int[] arr = { 3, 4, 12 };
      // loop through the byte array
      for (int i = 0; i < Buffer.ByteLength(arr); i++) {
         Console.WriteLine(Buffer.GetByte(arr, i));
      }
   }
}

Output

3
0
0
0
4
0
0
0
12
0
0
0

Updated on: 22-Jun-2020

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements