Buffer GetByte Example in C#

The Buffer.GetByte() method in C# allows you to read individual bytes from an array at a specified byte position. This method is useful when you need to examine the binary representation of data stored in arrays of primitive types.

Syntax

Following is the syntax for the Buffer.GetByte() method −

public static byte GetByte(Array array, int index)

Parameters

  • array − The source array from which to read the byte.
  • index − The zero-based byte position within the array.

Return Value

Returns a byte value at the specified position in the array.

Using Buffer.GetByte() with Integer Array

When working with integer arrays, each integer occupies 4 bytes in memory. The GetByte() method reads one byte at a time from this memory representation −

Memory Layout of int[] {3, 4, 12} 3 0 0 0 4 0 0 0 12 0 0 int value: 3 int value: 4 int value: 12 0 1 2 3 4 5 6 7 8 9 10 Byte indices Each int uses 4 bytes (little-endian format)

Example

using System;

public class Demo {
   public static void Main() {
      int[] arr = { 3, 4, 12 };
      
      Console.WriteLine("Array length: " + arr.Length + " elements");
      Console.WriteLine("Byte length: " + Buffer.ByteLength(arr) + " bytes");
      Console.WriteLine("Reading individual bytes:");
      
      // loop through the byte array
      for (int i = 0; i < Buffer.ByteLength(arr); i++) {
         Console.WriteLine("Byte at index " + i + ": " + Buffer.GetByte(arr, i));
      }
   }
}

The output of the above code is −

Array length: 3 elements
Byte length: 12 bytes
Reading individual bytes:
Byte at index 0: 3
Byte at index 1: 0
Byte at index 2: 0
Byte at index 3: 0
Byte at index 4: 4
Byte at index 5: 0
Byte at index 6: 0
Byte at index 7: 0
Byte at index 8: 12
Byte at index 9: 0
Byte at index 10: 0
Byte at index 11: 0

Using Buffer.GetByte() with Different Array Types

Example

using System;

public class BufferExample {
   public static void Main() {
      // Short array (2 bytes per element)
      short[] shortArray = { 256, 512 };
      Console.WriteLine("Short array bytes:");
      for (int i = 0; i < Buffer.ByteLength(shortArray); i++) {
         Console.WriteLine("Byte " + i + ": " + Buffer.GetByte(shortArray, i));
      }
      
      Console.WriteLine();
      
      // Byte array (1 byte per element)
      byte[] byteArray = { 65, 66, 67 };
      Console.WriteLine("Byte array bytes:");
      for (int i = 0; i < Buffer.ByteLength(byteArray); i++) {
         Console.WriteLine("Byte " + i + ": " + Buffer.GetByte(byteArray, i));
      }
   }
}

The output of the above code is −

Short array bytes:
Byte 0: 0
Byte 1: 1
Byte 2: 0
Byte 3: 2

Byte array bytes:
Byte 0: 65
Byte 1: 66
Byte 2: 67

How It Works

The Buffer.GetByte() method treats the array as a contiguous block of memory and reads one byte at the specified position. For multi-byte data types like int or short, the method reveals the internal byte representation, typically in little-endian format where the least significant byte comes first.

Conclusion

The Buffer.GetByte() method provides low-level access to the byte representation of arrays in C#. It is particularly useful for understanding memory layout, performing binary operations, or when you need to examine data at the byte level for serialization or debugging purposes.

Updated on: 2026-03-17T07:04:35+05:30

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements