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.
Before EnsureCapacity method capacity = 16 After EnsureCapacity method capacity = 18
Before EnsureCapacity method capacity = 16 After EnsureCapacity method capacity = 44
- Difference between Method Overriding and Method Hiding in C#
- DateTime.Add() Method in C#
- C# | Math.Log() Method
- C# | ToUpper() Method
- C# | ToLower() Method
- C# | Method Overloading
- C# | Math.Abs() Method | Set - 1
- DateTimeOffset.Add() Method in C#
- C# | Math.Abs() Method | Set - 2
- Extension Method in C#
- C# | Uri.GetHashCode() Method
- C# | Method Parameters
- C# | Math.Exp() Method
- C# | SortedDictionary.Add() Method
- C# | StartsWith() Method
- Anonymous Method in C#
- C# | Uri.IsHexEncoding() Method
- C# | CompareOrdinal() Method
- C# | Copy() Method
- C# | Math.Pow() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.