Assume that I have a MemoryStream and function that operates on bytes.
Current code is something like this:
void caller()
{
MemoryStream ms = // not important
func(ms.GetBuffer(), 0, (int)ms.Length);
}
void func(byte[] buffer, int offset, int length)
{
// not important
}
I can not change func but I would like to minimize possibility of changing stream data from within the func.
How could / should I rewrite the code to be sure that stream data won’t be changed?
Or this can’t be done?
EDIT:
I am sorry, I didn’t mention that a I would like to not make copies of data.
Call
.ToArray.From MSDN (emphasis mine):
Ideally you would change
functo take anIEnumerable<byte>. Once a method has the array, you’re trusting they won’t modify the data if you don’t want them to. If the contract was to provideIEnumerable<byte>, the implementer would have to decide if they need a copy to edit or not.