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

  • SEARCH
  • Home
  • 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 210433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:00:17+00:00 2026-05-11T18:00:17+00:00

Is there a best (see below) way to append two byte arrays in C#?

  • 0

Is there a best (see below) way to append two byte arrays in C#?

Pretending I have complete control, I can make the first byte array sufficiently large to hold the second byte array at the end and use the Array.CopyTo function. Or I can loop over individual bytes and make an assignment.

Are there better ways? I can’t imagine doing something like converting the byte arrays to string and joining them and converting them back would be better than either method above.

In terms of best/better (in order):

  1. Fastest
  2. Least RAM consumption

A constraint is that I must work in the .NET 2.0 framework.

The two choices recommended are MemoryStream and BlockCopy. I have run a simple speed test of 10,000,000 loops 3 times and got the following results:

Average of 3 runs of 10,000,000 loops in milliseconds:

  • BlockCopy Time: 1154, with a range of 13 milliseconds
  • MemoryStream GetBuffer Time: 1470, with a range of 14 milliseconds
  • MemoryStream ToArray Time: 1895, with a range of 3 milliseconds
  • CopyTo Time: 2079, with a range of 19 milliseconds
  • Byte-by-byte Time: 2203, with a range of 10 milliseconds

Results of List<byte> AddRange over 10 million loops:
List<byte> Time: 16694

Relative RAM Consumption (1 is baseline, higher is worse):

  • Byte-by-byte: 1
  • BlockCopy: 1
  • Copy To: 1
  • MemoryStream GetBuffer: 2.3
  • MemoryStream ToArray: 3.3
  • List<byte>: 4.2

The test shows that in general, unless you are doing a lot of byte copies [which I am], looking at byte copies is not worth a focus [e.g. 10 million runs yielding a difference of as much as 1.1 seconds].

  • 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. Editorial Team
    Editorial Team
    2026-05-11T18:00:18+00:00Added an answer on May 11, 2026 at 6:00 pm

    You want BlockCopy

    According to this blog post it is faster than Array.CopyTo.

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

Sidebar

Related Questions

Is there a best-practice or common way in JavaScript to have class members as
I have a small list of checkboxes (see below), and I noticed I can
What is the best method for user authorisation/authentication in ASP.NET MVC? I see there
Is there a best way to turn an integer into its month name in
I am wondering if there is a best way to shuffle a list of
Whenever I design a database, I always wonder if there is a best way
Whats is the best way to obtain the content between two strings e.g. ob_start();
I have a list of around 30 divs (see below.) and would like to
Here's the scenario... I have a table of subnets. (see below) I have an
Okay, here's the situation. I have two NSManagedObjects called Store and Aisle . There

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.