.NET Framework Class Library  

MemoryStream Class

Creates a stream whose backing store is memory.

For a list of all members of this type, see MemoryStream Members.

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         System.IO.MemoryStream

[Visual Basic]
<Serializable>
Public Class MemoryStream
   Inherits Stream
[C#]
[Serializable]
public class MemoryStream : Stream
[C++]
[Serializable]
public __gc class MemoryStream : public Stream
[JScript]
public
   Serializable
class MemoryStream extends Stream

Thread Safety

Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application.

The current position of a stream is the position at which the next read or write operation could take place. The current position can be retrieved or set through the Seek method. When a new instance of MemoryStream is created, the current position is set to zero.

Memory streams created with an unsigned byte array provide a non-resizable stream view of the data, and can only be written to. When using a byte array, you can neither append to nor shrink the stream, although you might be able to modify the existing contents depending on the parameters passed into the constructor. Empty memory streams are resizable, and can be written to and read from.

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET

Assembly: Mscorlib (in Mscorlib.dll)

See Also

MemoryStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File