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 memory allocated by the current instance. Its value can range from Length to MaxCapacity.
Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less than the current length of this instance or the value specified for a set operation is greater than the maximum capacity.
Below programs will illustrate the use of the above-discussed property:
Example 1:
// C# program to demonstrate // the Capacity() Property using System; using System.Text; class GFG { // Main Method public static void Main(String[] args) { // create a StringBuilder object, // default capacity will be 16 StringBuilder str = new StringBuilder(); // get default capacity int cap = str.Capacity; Console.WriteLine("Deafult Capacity of StringBuilder = " + cap); // add the String to StringBuilder Object str.Append("Geek"); // get capacity cap = str.Capacity; // print the result Console.WriteLine("StringBuilder = " + str); Console.WriteLine("Current Capacity of StringBuilder = " + cap); } } |
Deafult Capacity of StringBuilder = 16 StringBuilder = Geek Current Capacity of StringBuilder = 16
Example 2:
// C# program to demonstrate // the Capacity() Property using System; using System.Text; class GFG { public static void Main(String[] args) { // create a StringBuilder object // with a String passed as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // get capacity int capacity = str.Capacity; // print the result Console.WriteLine("StringBuilder = " + str); Console.WriteLine("Capacity of StringBuilder = " + capacity); } } |
StringBuilder = WelcomeGeeks Capacity of StringBuilder = 16
Reference:
Recommended Posts:
- C# | Creating StringBuilder having specified capacity
- How to find the MaxCapacity of a StringBuilder in C#
- How to find the length of the StringBuilder in C#
- C# | Capacity of a List
- C# | Capacity of a SortedList
- C# | Creating an ArrayList having specified initial capacity
- StringBuilder in C#
- C# | Sets the capacity to the actual number of elements in the ArrayList
- C# | String vs StringBuilder
- How to create the StringBuilder in C#
- C# | Setting the capacity to the actual number of elements in a SortedList object
- StringBuilder.CopyTo Method in C#
- StringBuilder.EnsureCapacity() Method in C#
- StringBuilder.ToString 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 contribute@geeksforgeeks.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.



