Array.Copy
Buffer.BlockCopy
C# programming
data copying methods
.NET framework

Array.Copy vs Buffer.BlockCopy

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Array.Copy and Buffer.BlockCopy both copy data in .NET, but they operate at different levels. Array.Copy works in terms of array elements and type compatibility. Buffer.BlockCopy works in terms of raw bytes inside primitive arrays. If you choose the wrong one, the code may be slower, harder to read, or simply incorrect because offsets and lengths mean different things.

Array.Copy Copies Elements

Array.Copy is the general-purpose choice. You specify source and destination arrays, element indexes, and an element count.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] source = { 10, 20, 30, 40 };
8        int[] destination = new int[4];
9
10        Array.Copy(source, 1, destination, 0, 3);
11        Console.WriteLine(string.Join(", ", destination));
12    }
13}

That copies the elements 20, 30, and 40 into the destination array. The units are elements, not bytes.

This method is appropriate when:

  • you are copying arrays of reference types or structs
  • you care about readability and type safety
  • offsets should be expressed in element positions

Buffer.BlockCopy Copies Bytes

Buffer.BlockCopy is lower level. It only works with arrays of primitive types, and the offsets and counts are in bytes.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        int[] source = { 1, 2, 3, 4 };
8        int[] destination = new int[4];
9
10        Buffer.BlockCopy(source, 0, destination, 0, source.Length * sizeof(int));
11        Console.WriteLine(string.Join(", ", destination));
12    }
13}

That works because int is a primitive type and the byte count is calculated explicitly. If you pass source.Length as the last argument, you would only copy four bytes, not four integers.

That byte-oriented behavior is the main reason people misuse BlockCopy.

When BlockCopy Is the Better Tool

Buffer.BlockCopy is useful when you genuinely want raw memory-style movement between primitive arrays, especially for binary protocols or serialization-like work.

For example, copying bytes into an int[] or pulling a chunk out of a byte[] can be natural with BlockCopy because the operation is already byte-based.

csharp
1byte[] bytes = { 1, 0, 0, 0, 2, 0, 0, 0 };
2int[] numbers = new int[2];
3Buffer.BlockCopy(bytes, 0, numbers, 0, bytes.Length);
4Console.WriteLine(string.Join(", ", numbers));

That is a different use case from ordinary array slicing.

Choosing the Right API

A simple rule works well:

  • if you think in elements, use Array.Copy
  • if you think in bytes, use Buffer.BlockCopy

In newer .NET code, you should also be aware of spans. Span<T> and Memory<T> often provide a cleaner modern alternative for high-performance slicing and copying without some of the awkwardness of older APIs.

Still, between these two APIs, Array.Copy is the default choice for everyday code because it communicates intent more clearly. If another developer has to stop and recalculate byte offsets just to understand the copy, that extra complexity should usually be justified by a real low-level need.

Common Pitfalls

  • Using Buffer.BlockCopy with non-primitive arrays.
  • Forgetting that srcOffset, dstOffset, and count are measured in bytes, not elements.
  • Choosing BlockCopy for tiny arrays where the readability cost outweighs any performance benefit.
  • Assuming Array.Copy and Buffer.BlockCopy are interchangeable because both "copy arrays".
  • Ignoring newer abstractions such as spans when writing performance-sensitive modern .NET code.

Summary

  • 'Array.Copy copies array elements and is the general-purpose API.'
  • 'Buffer.BlockCopy copies raw bytes and is limited to primitive-type arrays.'
  • The biggest difference is units: Array.Copy uses element counts, BlockCopy uses byte counts.
  • Use BlockCopy for byte-oriented binary work, not for ordinary array manipulation by default.
  • When in doubt, prefer the clearer API unless profiling shows a real need for lower-level copying.

Course illustration
Course illustration

All Rights Reserved.