StringBuilder(Int32) constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the specified initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the existing string object.
public StringBuilder (int capacity);
Here, capacity is the starting size of the StringBuilder instance. If the number of characters which is to be stored becomes greater than the specified capacity then the StringBuilder object will dynamically allocate the memory to store them.
Exception: This will give ArgumentOutOfRangeException if the capacity is less than zero.
Capacity of StringBuilder: 10
Example 2: For ArgumentOutOfRangeException
System.ArgumentOutOfRangeException: ‘capacity’ must be greater than zero.
Parameter name: capacity
- C# | Creating an ArrayList having specified initial capacity
- How to find the Capacity of a StringBuilder in C#
- C# | Creating a HybridDictionary with specified initial size & case sensitivity
- C# | Creating an empty HybridDictionary with specified case sensitivity
- C# | Creating a Case-Sensitive HybridDictionary with specified initial size
- Creating an Index From the Specified Index at the Start of a Collection in C#
- Creating an Index From the End of a Collection at a Specified Index Position in C#
- C# | Capacity of a List
- C# | Sets the capacity to the actual number of elements in the ArrayList
- C# | Capacity of a SortedList
- C# | Setting the capacity to the actual number of elements in a SortedList object
- Getting the name of the Enumeration Constant having Specified value in C#
- StringBuilder.Chars Property in C#
- C# | String vs StringBuilder
- How to find the length of the StringBuilder in C#
- How to find the MaxCapacity of a StringBuilder in C#
- C# | Check if two StringBuilder objects are Equal
- StringBuilder.EnsureCapacity() Method in C#
- StringBuilder.CopyTo Method in C#
- How to remove all characters from StringBuilder in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.