Buffer Type in C#

The Buffer class in C# provides efficient methods for manipulating arrays of primitive types at the byte level. It is particularly useful when you need to perform fast operations on large arrays or when working with binary data. The Buffer.BlockCopy method is the most commonly used method, which copies bytes from one array to another.

Syntax

Following is the syntax for the Buffer.BlockCopy method −

Buffer.BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count);

Parameters

  • src ? The source array from which bytes are copied

  • srcOffset ? The byte offset into the source array

  • dst ? The destination array to which bytes are copied

  • dstOffset ? The byte offset into the destination array

  • count ? The number of bytes to copy

Using Buffer.BlockCopy for Byte Arrays

Example

using System;

class Demo {
    static void Main() {
        // byte arrays
        byte[] b1 = new byte[] {39, 45, 58 };
        byte[] b2 = new byte[5];
        
        // copying bytes from one to another
        Buffer.BlockCopy(b1, 0, b2, 0, 3);
        
        Console.WriteLine("Source array:");
        DisplayArray(b1);
        Console.WriteLine("Destination array after copying:");
        DisplayArray(b2);
    }
    
    static void DisplayArray(byte[] arr) {
        for (int i = 0; i 

The output of the above code is −

Source array:
39 45 58 
Destination array after copying:
39 45 58 0 0 

Using Buffer.BlockCopy with Integer Arrays

The Buffer class can work with any primitive type array, not just byte arrays −

Example

using System;

class BufferIntExample {
    static void Main() {
        int[] source = {10, 20, 30, 40};
        int[] destination = new int[6];
        
        // Copy 8 bytes (2 integers) from source to destination
        Buffer.BlockCopy(source, 0, destination, 0, 8);
        
        Console.WriteLine("Source array:");
        foreach(int num in source) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
        
        Console.WriteLine("Destination array:");
        foreach(int num in destination) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Source array:
10 20 30 40 
Destination array:
10 20 0 0 0 0 

Buffer Methods Comparison

Method Purpose Return Type
Buffer.BlockCopy Copies bytes from one array to another void
Buffer.ByteLength Returns the number of bytes in an array int
Buffer.GetByte Retrieves a byte at a specified location byte
Buffer.SetByte Sets a byte at a specified location void

Using Buffer.ByteLength and Buffer.GetByte

Example

using System;

class BufferUtilityExample {
    static void Main() {
        int[] numbers = {100, 200, 300};
        
        // Get the byte length of the array
        int byteLength = Buffer.ByteLength(numbers);
        Console.WriteLine("Byte length of array: " + byteLength);
        
        // Get individual bytes
        Console.WriteLine("Individual bytes:");
        for(int i = 0; i 

The output of the above code is −

Byte length of array: 12
Individual bytes:
100 0 0 0 200 0 0 0 44 1 0 0 

Conclusion

The Buffer class in C# provides efficient low-level operations for manipulating primitive type arrays at the byte level. Buffer.BlockCopy is particularly useful for fast array copying operations, while other methods like Buffer.ByteLength and Buffer.GetByte allow fine-grained byte-level access to array data.

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

784 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements