StringBuilder.EnsureCapacity() Method in C# Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 capacity number of characters; otherwise, no memory is changed. Syntax: public int EnsureCapacity (int capacity); Here, the capacity is the minimum capacity to ensure. Return Value: It returns the new capacity of the current instance. Exception: This method will give ArgumentOutOfRangeException if the capacity is less than zero or the Enlarging the value of this instance would exceed MaxCapacity. Example 1: csharp // C# program to demonstrate // the EnsureCapacity Method using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object StringBuilder str = new StringBuilder(); // print string capacity Console.WriteLine("Before EnsureCapacity " + "method capacity = " + str.Capacity); // apply ensureCapacity() str.EnsureCapacity(18); // print string capacity Console.WriteLine("After EnsureCapacity" + " method capacity = " + str.Capacity); } } Output: Before EnsureCapacity method capacity = 16 After EnsureCapacity method capacity = 18 Example 2: csharp // C# program to demonstrate // the EnsureCapacity Method using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object StringBuilder str = new StringBuilder(); // print string capacity Console.WriteLine("Before EnsureCapacity " + "method capacity = " + str.Capacity); // apply ensureCapacity() str.EnsureCapacity(44); // print string capacity Console.WriteLine("After EnsureCapacity" + " method capacity = " + str.Capacity); } } Output: Before EnsureCapacity method capacity = 16 After EnsureCapacity method capacity = 44 Reference: https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.ensurecapacity?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article How to find the Capacity of a StringBuilder in C# K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-StringBuilder-Class Similar Reads StringBuilder.CopyTo Method in C# This method is used to copy the characters from a specified segment of this instance to a specified segment of a destination Char array. Syntax: public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count); Parameters: sourceIndex: It is the starting position in this ins 2 min read C# | Uri.EscapeDataString(String) Method Uri.EscapeDataString(String) Method is used to convert a string to its escaped representation. Syntax: public static string EscapeDataString (string stringToEscape); Here, it takes the string to escape. Return Value: This method returns a string which contains the escaped representation of stringToE 3 min read 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.ToString Method in C# This method is used to converts the value of this instance to a String. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by ToString(). Subsequent changes to this sequence contained by Object do not affect the con 2 min read C# | Creating StringBuilder having specified capacity 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 i 2 min read StringBuilder.Chars[] Property in C# StringBuilder.Chars[Int32] Property is used to get or set the character at the specified character position in this instance. Syntax: public char this[int index] { get; set; } Here, the index is the position of the character. Property Value: This property returns the Unicode character at position in 2 min read Like