Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a new array reference which contains a subset of an existing array, without copying memory?

I am building a file reader in C#, and large volumes of data will be enumerated. I want to use the same buffer for each element I read out, then pass the buffer on for further processing by the client. The API would be cleaner if I could return a byte[] of the correct size, rather than the raw buffer and a length.

Is it possible to do this in C# without copying memory?

like image 457
James L Avatar asked Dec 15 '22 15:12

James L


2 Answers

You can use ArraySegment<T>

http://msdn.microsoft.com/en-us/library/1hsbd92d.aspx

That lets you specify the start and end of the segment you want to pass on without copying any data.

like image 113
derkyjadex Avatar answered Dec 18 '22 05:12

derkyjadex


If you can change your API parameter types, I think you could use an ArraySegment.

like image 40
Matthew Watson Avatar answered Dec 18 '22 05:12

Matthew Watson