I\'ve known that GetBuffer()
on a MemoryStream in C#/.NET has to be used with care, because, as the docs describe here, there can be unused bytes at the end, so
GetBuffer()
always assumes you know the structure of the data fed into the string (and that's its use). If you want to get data out of the stream, you should always use one of the provided methods (e.g. ToArray()
).
Something like this can be used, but only case I could think of right now would be some fixed structure or virtual file system sitting in the stream. For example, at your current position you're reading an offset for a file sitting inside the stream. You then create a new stream object based on this stream's buffer but with the different _origin
. This saves you from copying the whole data for the new object, which might enable you to save lots of memory. This saves you from carrying the initial buffer as a reference with you, because you're always able to retrieve it once again.