KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have been looking around on ways to return the segment which is basically held by ArraySegment in terms of offset and count. Although ArraySegment holds the complete and original array, it just delimits it with the fact that any changes to the segment are reflected into the original. The problem or say the limitation with ArraySegment is that it will not return the segment itself as a whole and I have to traverse the values. What would be the best way to return the segment as a whole? byte[] input = new byte[5]{1,2,3,4,5}; ArraySegment<byte> delimited = new ArraySegment<byte>(input,0,2); byte[] segment = HERE I NEED SOMETHING THAT WILL RETURN THE SEGMENT i.e. [0,1,2] The most important point, segment must not be a copy but should refer to the original array. If any changes to segment are done, they must be reflected in the original array. Any tips are highly appreciated, thanks! ASSIGNMENT BENCHMARKS : After some answers from Thomas and digEmAll Ok, I ran some benchmarks against the code from digEmAll and Thomas, and to my surprise the code is overwhelmingly faster. Just what I was desperately looking for. Here are the results. Construct Size Elements assigned Iterations Time _______________________________________________________________________________ ArraySegmentWrapper 1500 1500 1000000 396.3 ms Array.Copy 1500 1500 1000000 4389.04 ms As you can see the whopping difference, it is very clear to me that I shall be using the code for ArraySegment. Below is the benchmarking code. Please note that this might be a bit biased as people would argue why "new" has been put inside a loop. I am just trying to reproduce the situat
Tags (comma-separated)
Save Edits
Cancel