views:

72

answers:

2

The .NET MemoryStream does not appear to have a .Reset or .Clear method.

I was thinking of using the following code to accomplish this:

ms.Seek(0, IO.SeekOrigin.Begin)
ms.SetLength(0)

What is the proper way to clear or reset an existing .NET MemoryStream?

+4  A: 

Why do you need resetting memory stream? You always can create a new one. Or you can use:

memoryStream.SetLength(0);
Andrew Bezzub
True, but does not answer my question. How can I reset my existing MemoryStream.
I think Setlength(0) should work for you.
Andrew Bezzub
+2  A: 

The memorystream does not have a reset/clear method because it would be redundant. By setting it to zero length you clear it.

Of course you could always do:

memoryStream = new MemoryStream(memoryStream.Capacity());

This would yield you a memorystream of the same size that is initialized.

If you really want to manually clear the stream I suspect you would have to resort to looping through the elements.

omglolbah