StringBuilder Constructors

Definition

Initializes a new instance of the StringBuilder class.

Overloads

StringBuilder()

Initializes a new instance of the StringBuilder class.

StringBuilder(Int32)

Initializes a new instance of the StringBuilder class using the specified capacity.

StringBuilder(String)

Initializes a new instance of the StringBuilder class using the specified string.

StringBuilder(Int32, Int32)

Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.

StringBuilder(String, Int32)

Initializes a new instance of the StringBuilder class using the specified string and capacity.

StringBuilder(String, Int32, Int32, Int32)

Initializes a new instance of the StringBuilder class from the specified substring and capacity.

StringBuilder()

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class.

public StringBuilder ();

Examples

The following example demonstrates how to call the StringBuilder constructor with no parameters.

StringBuilder stringBuilder = new StringBuilder();

Remarks

The string value of this instance is set to String.Empty, and the capacity is set to the implementation-specific default capacity.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringBuilder(Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified capacity.

public StringBuilder (int capacity);

Parameters

capacity
Int32

The suggested starting size of this instance.

Exceptions

capacity is less than zero.

Examples

The following example demonstrates how to call the StringBuilder constructor with a specified capacity.

int capacity = 255;
StringBuilder stringBuilder = new StringBuilder(capacity);

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

The string value of this instance is set to String.Empty. If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringBuilder(String)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified string.

public StringBuilder (string value);
public StringBuilder (string? value);

Parameters

value
String

The string used to initialize the value of the instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

Examples

The following example demonstrates how to call the StringBuilder constructor with the specified string.

string initialString = "Initial string.";
StringBuilder stringBuilder = new StringBuilder(initialString);

Remarks

If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringBuilder(Int32, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.

public StringBuilder (int capacity, int maxCapacity);

Parameters

capacity
Int32

The suggested starting size of the StringBuilder.

maxCapacity
Int32

The maximum number of characters the current string can contain.

Exceptions

maxCapacity is less than one, capacity is less than zero, or capacity is greater than maxCapacity.

Examples

The following example demonstrates how to call the StringBuilder constructor with a specified capacity and maximum capacity.

int capacity = 255;
int maxCapacity = 1024;
StringBuilder stringBuilder = 
    new StringBuilder(capacity, maxCapacity);

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

The maxCapacity property defines the maximum number of characters that the current instance can hold. Its value is assigned to the MaxCapacity property. If the number of characters to be stored in the current instance exceeds this maxCapacity value, the StringBuilder object does not allocate additional memory, but instead throws an exception.

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringBuilder(String, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified string and capacity.

public StringBuilder (string value, int capacity);
public StringBuilder (string? value, int capacity);

Parameters

value
String

The string used to initialize the value of the instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

capacity
Int32

The suggested starting size of the StringBuilder.

Exceptions

capacity is less than zero.

Examples

The following example demonstrates how to call the StringBuilder constructor with an initial string and a specified capacity.

string initialString = "Initial string. ";
int capacity = 255;
StringBuilder stringBuilder = 
    new StringBuilder(initialString, capacity);

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

StringBuilder(String, Int32, Int32, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class from the specified substring and capacity.

public StringBuilder (string value, int startIndex, int length, int capacity);
public StringBuilder (string? value, int startIndex, int length, int capacity);

Parameters

value
String

The string that contains the substring used to initialize the value of this instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

startIndex
Int32

The position within value where the substring begins.

length
Int32

The number of characters in the substring.

capacity
Int32

The suggested starting size of the StringBuilder.

Exceptions

capacity is less than zero.

-or-

startIndex plus length is not a position within value.

Examples

The following example demonstrates how to call the StringBuilder constructor with the specified string.

string initialString = "Initial string for stringbuilder.";
int startIndex = 0;
int substringLength = 14;
int capacity = 255;
StringBuilder stringBuilder = new StringBuilder(initialString, 
    startIndex, substringLength, capacity);

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0