How to determinate the size of buffer

A

ad

I want to write a MemoryStream to a buffer.
I use the codes below;
The code is from the msdn help,
They always declare the size of buffer as the (length of memorystream)+1;
The code run ok, but if I declare the size of buffer as the (length of
memorystream),
it run ok too.

How to determinate the size of buffer of memorystream?






//
Code---------------------------------------------------------------------------------------------------
DataSet ds = Will.DM.ExecuteDataset("Select * from customer");
MemoryStream ms1 = new MemoryStream();
ds.WriteXml(ms1);

DataSet ds2 = new DataSet();

int iLen = (int)ms1.Length;
byte[] buffer1 = new byte[iLen+1];


ms1.Position = 0;
ms1.Read(buffer1, 0, iLen);
 
N

Nicholas Paldino [.NET/C# MVP]

You don't need to allocate a new array. All you have to do is call the
ToArray method on the MemoryStream and it will return an array that is a
copy of the contents in the stream (properly sized, I might add).

Your code will work without the + 1. I don't know where that's from.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top