Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:24:58+00:00 2026-05-11T01:24:58+00:00

I have 3 byte arrays in C# that I need to combine into one.

  • 0

I have 3 byte arrays in C# that I need to combine into one. What would be the most efficient method to complete this task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:24:58+00:00Added an answer on May 11, 2026 at 1:24 am

    For primitive types (including bytes), use System.Buffer.BlockCopy instead of System.Array.Copy. It’s faster.

    I timed each of the suggested methods in a loop executed 1 million times using 3 arrays of 10 bytes each. Here are the results:

    1. New Byte Array using System.Array.Copy – 0.2187556 seconds
    2. New Byte Array using System.Buffer.BlockCopy – 0.1406286 seconds
    3. IEnumerable<byte> using C# yield operator – 0.0781270 seconds
    4. IEnumerable<byte> using LINQ’s Concat<> – 0.0781270 seconds

    I increased the size of each array to 100 elements and re-ran the test:

    1. New Byte Array using System.Array.Copy – 0.2812554 seconds
    2. New Byte Array using System.Buffer.BlockCopy – 0.2500048 seconds
    3. IEnumerable<byte> using C# yield operator – 0.0625012 seconds
    4. IEnumerable<byte> using LINQ’s Concat<> – 0.0781265 seconds

    I increased the size of each array to 1000 elements and re-ran the test:

    1. New Byte Array using System.Array.Copy – 1.0781457 seconds
    2. New Byte Array using System.Buffer.BlockCopy – 1.0156445 seconds
    3. IEnumerable<byte> using C# yield operator – 0.0625012 seconds
    4. IEnumerable<byte> using LINQ’s Concat<> – 0.0781265 seconds

    Finally, I increased the size of each array to 1 million elements and re-ran the test, executing each loop only 4000 times:

    1. New Byte Array using System.Array.Copy – 13.4533833 seconds
    2. New Byte Array using System.Buffer.BlockCopy – 13.1096267 seconds
    3. IEnumerable<byte> using C# yield operator – 0 seconds
    4. IEnumerable<byte> using LINQ’s Concat<> – 0 seconds

    So, if you need a new byte array, use

    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); 

    But, if you can use an IEnumerable<byte>, DEFINITELY prefer LINQ’s Concat<> method. It’s only slightly slower than the C# yield operator, but is more concise and more elegant.

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

    If you have an arbitrary number of arrays and are using .NET 3.5, you can make the System.Buffer.BlockCopy solution more generic like this:

    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; } 

    *Note: The above block requires you adding the following namespace at the the top for it to work.

    using System.Linq; 

    To Jon Skeet’s point regarding iteration of the subsequent data structures (byte array vs. IEnumerable<byte>), I re-ran the last timing test (1 million elements, 4000 iterations), adding a loop that iterates over the full array with each pass:

    1. New Byte Array using System.Array.Copy – 78.20550510 seconds
    2. New Byte Array using System.Buffer.BlockCopy – 77.89261900 seconds
    3. IEnumerable<byte> using C# yield operator – 551.7150161 seconds
    4. IEnumerable<byte> using LINQ’s Concat<> – 448.1804799 seconds

    The point is, it is VERY important to understand the efficiency of both the creation and the usage of the resulting data structure. Simply focusing on the efficiency of the creation may overlook the inefficiency associated with the usage. Kudos, Jon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer MD5 is the way to go. May 11, 2026 at 1:50 pm
  • added an answer In this line: destination = Vector2(*event.pos) – Vector2(*sprite.get_size())/2. You somehow… May 11, 2026 at 1:50 pm
  • added an answer A dynamically allocated matrix is better: can be resized to… May 11, 2026 at 1:50 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.