Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MemoryStream.Capacity Property

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public virtual int Capacity { get; set; }
/** @property */
public int get_Capacity ()

/** @property */
public void set_Capacity (int value)

public function get Capacity () : int

public function set Capacity (value : int)

Not applicable.

Property Value

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

Exception typeCondition

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.

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.

This code example is part of a larger example provided for the MemoryStream class.

// 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());

// Write the stream properties to the console.
Console.WriteLine(
    "Capacity = {0}, Length = {1}, Position = {2}\n", 
    (new Integer( memStream.get_Capacity())).ToString(),
    (new Long ( memStream.get_Length())).ToString(),
    (new Long ( memStream.get_Position())).ToString());

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft