1

Is it possible in C# to make temporary sub-array on top of existent array without memory allocations/copying?

GLaz
  • 295
  • 1
  • 11

1 Answers1

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