Hi Daniel,
Oh, maybe my original statement is somewhat not precise. I think the reason
the implement of MemoryStream.Dispose did not do everything .Close() does
is that IDisposable.Dispose is used for freeing, releasing, or resetting
unmanaged resources. So it will not do the extra Stream-related work(Such
as control the stream state etc.) of MemoryStream.
For the "the buffer is still available on a MemoryStream once the stream
has been closed", it is actully correct.
MemoryStream.Close only "Closes the stream for reading and writing.", in
which reading means memorystream can read bytes from the stream into your
buffer, writing means that you can write your buffer's bytes into that
stream.
After Closed, you can not do these 2 operations, but because the buffer
under your memorystream class is a managed resource, it has not been
released by Dispose method, it will still been available, so you still can
read bytes from the buffer to your application, I think it will only be
unavailable after this memorystream object was GC collected.
The code snippet below demonstrated this:
private MemoryStream ms;
//Construct the MemoryStream in Form_Load
private void Form1_Load(object sender, System.EventArgs e)
{
int count=10;
byte[] buf=new byte[count];
for(int i=0;i<count;i++)
{
buf
=Convert.ToByte(count-i);
}
MemoryStream memstr=new MemoryStream();
this.ms=memstr;
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
int newcount=7;
byte[] newbuf=new byte[newcount];
newbuf=ms.GetBuffer();
for(int i=0;i<newcount;i++)
{
Console.Write(newbuf.ToString());
}
ms.Close();
newbuf=ms.GetBuffer(); //this works well
ms.Read(newbuf,0,newcount); //this line
will generate exception, because ms was closed for read.
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.