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#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | Substring() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() Method