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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:59:04+00:00 2026-06-16T02:59:04+00:00

There are many questions that ask how to get a subset of an array

  • 0

There are many questions that ask how to get a subset of an array on this site, however no one has successfully gotten an answer on how to write to the array as if it were byte[].

byte[] bigTargetBytes = new byte[10000];

byte[] twentyBytes = sha256.ComputeHash(sourcebytes);  // size is 20 bytes, 
// how do I append this to the existing array without copying, using Linq, etc?

What I don’t want to do is have two arrays and do a copy between them, since that is time intensive proportional to the length of the array (and my arrays are long)

  • 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-16T02:59:05+00:00Added an answer on June 16, 2026 at 2:59 am

    It’s not exactly clear what you want, but I’m guessing you want a “view” onto an array of bytes. In .Net 4.5, you can do this with ArraySegment<T>:

    var a = new byte[100];
    var s = new ArraySegment<Byte>(a, 9, 90);
    
    ((IList<Byte>)s)[0] = 10;
    Console.WriteLine(a[9]);  // writes "10"
    

    So, your Write90Bytes method could return an ArraySegment<Byte> and then you could operate on that as a view into the underlying array, which would avoid the copy. Note, however, that ArraySegment<T> is a System.ValueType, which means casting it to IList<T> will incur a boxing penalty.

    If performance is critical and you need to avoid copying, you might also consider using pointers in an unsafe context, which means your Write90Bytes method could return a Byte* and you could then index into the array that way.

    EDIT: now that I see your edit, you can’t just return a value from a function “into” the memory location of an array. You’d need to pass the original array as an argument.

    EDIT 2: you can’t do what you are asking. The ComputeHash function returns an array which it itself allocates. The only way you could do what you want is if ComputeHash took the array as a parameter. Consider:

    // previously defined...
    byte[] bigTargetBytes = new byte[10000];      
    
    // This method doesn't really exist, but if it did it would look like this
    sha256.ComputeHash(sourcebytes, bigTargetBytes, offset); 
    

    Although in .Net there are specific restrictions on memory allocation due to it being a managed runtime, this is a common pattern in any programming language. In C/C++ development, there is a protocol used where either you pass in a pre-allocated array with an offset and maxlength to write to, or you allow the function to return an allocated memory which you are then given the responsibility to free (unless you are being called back from a framework). You can’t just return a value into an existing buffer.

    Do note however, that just copying 20 bytes from a GC array allocated by the ComputeHash value is a very trivial operation, which the runtime handles very efficiently. Unless you are very sensitive to microsecond level pauses from time to time, you won’t notice the difference between the actual copy and your hypothetical implementation.

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

Sidebar

Related Questions

There are many questions like this but I can't find one that seems to
I know that there are many different questions about this sort of topic on
There are many similar questions, but I didn't find one that gets straight to
There are many questions that ask how to change the class of a div
NOTE: I know there are many questions that talked about that but I'm still
I realize that there are many similar questions, but most involve scaling down the
There are many similar questions, but apparently no perfect match, that's why I'm asking.
First of all: I do know that there are already many questions and answers
I know that there are many Delphi database related questions available, but I'm considering
This question has been brought up many times, but I'd like to ask it

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.