Efficient Byte Array Concatenation in C#: Performance Analysis and Best Practices

Nov 20, 2025 · Programming · 10 views · 7.8

Keywords: C# | Byte Arrays | Performance Optimization | System.Buffer.BlockCopy | LINQ Concat

Abstract: This article provides an in-depth exploration of various methods for concatenating multiple byte arrays in C#, comparing the efficiency differences between System.Buffer.BlockCopy, System.Array.Copy, LINQ Concat, and yield operator through comprehensive performance test data. The analysis covers performance characteristics across different data scales and offers optimization recommendations for various usage scenarios, including trade-offs between immediate copying and deferred execution, memory allocation efficiency, and practical implementation best practices.

Introduction

Concatenating multiple byte arrays is a common requirement in C# programming, particularly in scenarios involving network data processing, file I/O operations, and memory management. Selecting the appropriate concatenation method not only impacts code readability but directly influences application performance. This article systematically analyzes the efficiency characteristics of various byte array concatenation methods based on comprehensive performance test data.

Performance Testing Environment and Methodology

To accurately evaluate the performance differences among various concatenation methods, we designed multiple test rounds using byte arrays of different scales for benchmarking. The testing environment included:

Major Concatenation Methods Comparison

System.Buffer.BlockCopy Method

System.Buffer.BlockCopy is a high-efficiency copying method specifically designed for primitive types, including bytes. Compared to System.Array.Copy, it demonstrates significant performance advantages when handling byte arrays.

Basic usage example:

byte[] rv = new byte[a1.Length + a2.Length + a3.Length];
System.Buffer.BlockCopy(a1, 0, rv, 0, a1.Length);
System.Buffer.BlockCopy(a2, 0, rv, a1.Length, a2.Length);
System.Buffer.BlockCopy(a3, 0, rv, a1.Length + a2.Length, a3.Length);

For arbitrary numbers of arrays, a generic method can be written:

private byte[] Combine(params byte[][] arrays)
{
    byte[] rv = new byte[arrays.Sum(a => a.Length)];
    int offset = 0;
    foreach (byte[] array in arrays) {
        System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
        offset += array.Length;
    }
    return rv;
}

LINQ Concat Method

The LINQ Concat method provides a concise and elegant approach to array concatenation, particularly suitable for scenarios requiring deferred execution.

Usage example:

IEnumerable<byte> rv = a1.Concat(a2).Concat(a3);

This method returns an IEnumerable<byte> sequence that supports deferred execution, making it highly efficient when only traversal of results is needed without immediate creation of a complete array.

C# Yield Operator

Using the yield operator enables creation of custom iterators, achieving deferred execution effects similar to LINQ Concat.

Performance Test Results Analysis

Small Array Performance Comparison

For small arrays of 10 bytes, test results show:

Medium-Scale Array Performance

When array size increases to 1,000 bytes:

Large-Scale Array Performance

For large arrays of 1 million bytes:

Comprehensive Efficiency of Creation and Usage

An important finding from performance testing is that comparing creation efficiency alone can be misleading. When considering subsequent iteration usage of the resulting data structure, the situation changes significantly.

In comprehensive tests including iteration:

This indicates that while deferred execution methods are highly efficient during the creation phase, they may incur significant performance overhead during subsequent iteration usage.

Semantic Differences and Application Scenarios

Immediate Copying vs Deferred Execution

Different concatenation methods exhibit important semantic differences:

Memory Considerations

Immediate copying methods require allocation of contiguous memory space equal to the total concatenated size, while deferred execution methods access data on-demand during iteration, offering more flexible memory usage.

Best Practice Recommendations

When Byte Arrays Are Required

If the application requires actual byte arrays:

Suitable Scenarios for Deferred Execution

If IEnumerable<byte> is acceptable:

Performance Optimization Considerations

In practical applications, selecting concatenation methods should consider:

Conclusion

The optimal method for byte array concatenation in C# depends on specific application requirements. For scenarios requiring immediate byte arrays, System.Buffer.BlockCopy provides the best performance. For scenarios accepting deferred execution, LINQ's Concat method offers good performance while maintaining code conciseness. Developers should select the most appropriate concatenation strategy based on data scale, usage patterns, and performance requirements.

Understanding the differences in semantic and performance characteristics among various methods is crucial to avoid one-sided optimization based solely on creation time. In practical projects, we recommend validating performance in specific scenarios through benchmarking to ensure selected methods meet functional requirements while providing acceptable performance characteristics.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.