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 8494449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:18:07+00:00 2026-06-10T23:18:07+00:00

I am using c# 4.0 and every so often I come across a method

  • 0

I am using c# 4.0 and every so often I come across a method in the .NET Common Library, or some other library, that has a signature like the following (e.g. Socket.Receive, Stream.Read, etc)

int DoSomethingSuperClever(byte[] buffer, int offset, int count)

The intention is always that you pass it in a buffer and it fills that buffer up to the maximum number of bytes you specify in the count argument (from the given offset) and returns exactly how many bytes it has really managed to fill in.

This is my, super naive, way of dealing with this situation:

var data = new byte[0];
var buffer = new byte[1024];
int read;

while ((read = something.DoSomethingSuperClever(buffer,
                                                0,
                                                buffer.Length)) > 0)
{
    int origLength = data.Length;
    var temp = new byte[origLength + read];
    Array.Copy(data, temp, data.Length);
    Array.Copy(buffer, 0, temp, origLength, read);
    data = temp;
}

return data;

I think that is pretty rubbish because of all the array creations but it does the job correctly at least I suppose.

I wondered about having a List<byte>, adding to it then doing ToArray at the end…

Of course, I cannot just call AddRange because then if read was less than the length of the buffer I would get junk appended (as AddRange doesn’t accept a length argument it will always add the entire collection). So then I think to go with that approach I would end up with a for loop and loads of Add calls but surely that is even worse than the array copy isn’t it?

So, experts, what is the most efficient way that I should be dealing with these types of calls?

  • 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-06-10T23:18:08+00:00Added an answer on June 10, 2026 at 11:18 pm

    I’d use a MemoryStream instead of a byte[]:

    var data = new MemoryStream();
    var buffer = new byte[1024];
    int read = 0;
    
    while ((read = something.DoSomethingSuperClever(buffer,
                                                    0,
                                                    buffer.Length) > 0)
    {
        data.Write(buffer, 0, read);
    }
    
    return data.ToArray();
    

    Depending on the source of your something object you might want to adjust the buffer size to be more efficient.

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

Sidebar

Related Questions

I'm currently developing a server (using Java) that has to send quite often (every
I'm using C# and VB.NET. I often (every days...) add comments to existing source
I have a launchd daemon that every so often uploads some data via a
Every so often I run across someone using (!~val) instead of (val === -1)
This is a question about the VB.NET language. Since I am using it every
I have an application that I restart at 2 am every morning using Application.Restart();
I've come across two different ways to define/name objects and functions in JavaScript that
We are using verisign's time stamp service currently, but every so often the time
I have a List that is cleared out every so often. The code is
I come from a python background, where it's often said that it's easier to

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.