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?
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.
If you can change your API parameter types, I think you could use an ArraySegment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With