StringBuilder.Capacity Property

 

Gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance.

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

member Capacity : int with get, set

Property Value

Type: System.Int32

The maximum number of characters that can be contained in the memory allocated by the current instance. Its value can range from Length to MaxCapacity.

Exception Condition
ArgumentOutOfRangeException

The value specified for a set operation is less than the current length of this instance.

-or-

The value specified for a set operation is greater than the maximum capacity.

Capacity does not affect the string value of the current instance. Capacity can be decreased as long as it is not less than Length.

The StringBuilder dynamically allocates more space when required and increases Capacity accordingly. For performance reasons, a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation-specific.

The following example demonstrates the Capacity property.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: