C# program to copy a range of bytes from one array to another

The Buffer.BlockCopy method in C# is used to copy a range of bytes from one array to another. This method provides a fast, low-level approach for copying data between arrays and is particularly useful when working with byte arrays or when you need to copy a specific range of elements.

Syntax

Following is the syntax for the Buffer.BlockCopy method −

Buffer.BlockCopy(sourceArray, sourceOffset, destinationArray, destinationOffset, count);

Parameters

  • sourceArray − The source array from which to copy bytes
  • sourceOffset − The zero-based byte offset into the source array
  • destinationArray − The destination array to copy bytes to
  • destinationOffset − The zero-based byte offset into the destination array
  • count − The number of bytes to copy

Buffer.BlockCopy Operation Source Array (b1) 22 49 [0] [1] Destination Array (b2) 22 49 0 0 0 [0] [1] [2] [3] [4] Copy 2 bytes Buffer.BlockCopy(b1, 0, b2, 0, 2); Copies 2 bytes from b1[0] to b2[0]

Using Buffer.BlockCopy with Byte Arrays

Example

using System;

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

The output of the above code is −

Source array (b1):
22 49 
Destination array before copy (b2):
0 0 0 0 0 
Destination array after copy (b2):
22 49 0 0 0 

Using Buffer.BlockCopy with Integer Arrays

Example

using System;

class Program {
    static void Main() {
        int[] source = {10, 20, 30, 40, 50};
        int[] destination = new int[7];
        
        Console.WriteLine("Source array:");
        DisplayIntArray(source);
        
        // Copy 3 integers (12 bytes) starting from index 1 in source to index 2 in destination
        Buffer.BlockCopy(source, sizeof(int) * 1, destination, sizeof(int) * 2, sizeof(int) * 3);
        
        Console.WriteLine("Destination array after copy:");
        DisplayIntArray(destination);
    }
    
    static void DisplayIntArray(int[] array) {
        foreach (int value in array) {
            Console.Write(value + " ");
        }
        Console.WriteLine();
    }
}

The output of the above code is −

Source array:
10 20 30 40 50 
Destination array after copy:
0 0 20 30 40 0 0 

Key Features

  • PerformanceBuffer.BlockCopy is optimized for fast copying operations
  • Byte-level copying − Works at the byte level, not element level
  • Type flexibility − Can copy between different primitive array types
  • Partial copying − Allows copying specific ranges of arrays

Conclusion

The Buffer.BlockCopy method provides an efficient way to copy bytes between arrays in C#. It works at the byte level and is particularly useful for copying ranges of data between arrays, whether they are byte arrays or other primitive type arrays.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements