Capacity Property
Collapse the table of content
Expand the table of content

MemoryStream.Capacity Property

Gets or sets the number of bytes allocated for this stream.

[Visual Basic]
Public Overridable Property Capacity As Integer
[C#]
public virtual int Capacity {get; set;}
[C++]
public: __property virtual int get_Capacity();
public: __property virtual void set_Capacity(int);
[JScript]
public function get Capacity() : int;
public function set Capacity(int);

Property Value

The length of the usable portion of the buffer for the stream.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException A capacity is set that is negative or less than the current length of the stream.
ObjectDisposedException The current stream is closed.
NotSupportedException set is invoked on a stream whose capacity cannot be modified.

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.

Capacity is the buffer length for system-provided byte arrays. Capacity cannot be set to a value less than the current length of the stream.

Example

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

[Visual Basic] 
' Write the stream properties to the console.
Console.WriteLine( _
    "Capacity = {0}, Length = {1}, Position = {2}", _
    memStream.Capacity.ToString(), _
    memStream.Length.ToString(), _
    memStream.Position.ToString())

[C#] 
// Write the stream properties to the console.
Console.WriteLine(
    "Capacity = {0}, Length = {1}, Position = {2}\n",
    memStream.Capacity.ToString(), 
    memStream.Length.ToString(), 
    memStream.Position.ToString());

[C++] 
// Write the stream properties to the console.
Console::WriteLine(S"Capacity = {0}, Length = {1}, "
    S"Position = {2}\n", memStream->Capacity.ToString(), 
    memStream->Length.ToString(), 
    memStream->Position.ToString());

[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.

Requirements

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 | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2016 Microsoft