C# | Creating StringBuilder having specified capacity Last Updated : 14 Mar, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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. Example 1: csharp // C# Program to illustrate how // to create a StringBuilder having // specified initial capacity using System; using System.Text; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // sb is the StringBuilder object // StringBuilder(10) is the constructor // used to initializes a new // instance of the StringBuilder class // having 10 as capacity StringBuilder sb = new StringBuilder(10); Console.Write("Capacity of StringBuilder: "); // using capacity property Console.WriteLine(sb.Capacity); } } Output: Capacity of StringBuilder: 10 Example 2: For ArgumentOutOfRangeException csharp // C# Program to illustrate how // to create a StringBuilder having // specified initial capacity using System; using System.Text; using System.Collections; class Geeks { // Main Method public static void Main(String[] args) { // sb is the StringBuilder object // taking capacity less than zero StringBuilder sb = new StringBuilder(-4); // using capacity property Console.WriteLine(sb.Capacity); } } Runtime Error: Unhandled Exception: System.ArgumentOutOfRangeException: 'capacity' must be greater than zero. Parameter name: capacity Reference: https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.-ctor?view=netframework-4.7.2#System_Text_StringBuilder__ctor_System_Int32_ Comment More infoAdvertise with us Next Article C# | Creating StringBuilder having specified capacity ankita_saini Follow Improve Article Tags : C# CSharp-StringBuilder-Class Similar Reads How to find the Capacity of a StringBuilder in C# StringBuilder.Capacity Property is used to get or set the maximum number of characters that can be contained in the memory allocated by the current instance. Syntax: public int Capacity { get; set; }Return Value: This property will return the maximum number of characters that can be contained in the 2 min read StringBuilder.EnsureCapacity() Method in C# The EnsureCapacity(Int32) method of StringBuilder class helps us to ensures the capacity is at least equal to the specified value that is passed as the parameter to the method. If the current capacity is less than the capacity parameter, memory for this instance is reallocated to hold at least capac 2 min read How to create the StringBuilder in C# StringBuilder() constructor is used to initialize a new instance of the StringBuilder class which will be empty and will have the default initial capacity. StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutabl 2 min read C# String vs StringBuilder 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 exis 3 min read How to find the MaxCapacity of a StringBuilder in C# StringBuilder.MaxCapacity Property is used to get the maximum capacity of this instance. Syntax: public int MaxCapacity { get; } Property Value: It returns the maximum number of characters of type System.Int32 this instance can hold. Note: The maximum capacity for this implementation is Int32.MaxVal 1 min read How to find the length of the StringBuilder in C# StringBuilder.Length Property is used to get or set the length of the current StringBuilder object. Syntax: public int Length { get; set; } It returns the length of the current instance. Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less 2 min read C# StringBuilder StringBuilder is a Dynamic Object. It doesnât create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.A String object is immutable, i.e. a String cannot be changed once created. To avoid string replacing, appending, removing or inserting 4 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read Create a string of specific length in C++ C++ has in its definition a way to represent a sequence of characters as an object of a class. This class is called std::string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte characters. This article focuses on discussing how t 3 min read Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i 5 min read Like