Is it possible in C# to make temporary sub-array on top of existent array without memory allocations/copying?
Asked
Active
Viewed 1,559 times
1 Answers
1
You can use the ArraySegment
structure:
Delimits a section of a one-dimensional array.
Take also a look at this question: what is the use of ArraySegment class?
Here's an example usage, stolen from Stephen Kennedy's answer:
var array = new byte[] { 5, 8, 9, 20, 70, 44, 2, 4 };
array.Dump();
var segment = new ArraySegment<byte>(array, 2, 3);
segment.Dump(); // output: 9, 20, 70
segment.Reverse().Dump(); // output 70, 20, 9
segment.Any(s => s == 99).Dump(); // output false
segment.First().Dump(); // output 9
array.Dump(); // no change

sloth
- 99,095
- 21
- 171
- 219
-
1new ArraySegment is already a allocation action – Fabian Kamp Jun 25 '21 at 12:27