This documentation is archived and is not being maintained.

MemoryStream Constructor (Int32)

.NET Framework 1.1

Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

[Visual Basic]
Public Sub New( _
   ByVal capacity As Integer _
public MemoryStream(
   int capacity
public: MemoryStream(
   int capacity
public function MemoryStream(
   capacity : int


The initial size of the internal array in bytes.


Exception Type Condition
ArgumentOutOfRangeException capacity is negative.


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 CanRead, CanSeek, and CanWrite properties are all set to true.

The capacity automatically increases when you use the SetLength method to set the length to a value larger than the capacity of the current stream. Except for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

This constructor exposes the underlying stream that GetBuffer returns.


[Visual Basic, C#, C++] This code example is part of a larger example provided for the MemoryStream class.

[Visual Basic] 
Dim memStream As New MemoryStream(100)

using(MemoryStream memStream = new MemoryStream(100))

MemoryStream* memStream = new MemoryStream(100);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


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, Common Language Infrastructure (CLI) Standard

See Also

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