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.MaxValue. However, this value is implementation-specific and might be different in other or later implementations. You can explicitly set the maximum capacity of a StringBuilder object by calling the StringBuilder(Int32, Int32) constructor.






// C# program to demonstrate
// the MaxCapacity Property
using System;
using System.Text;
class GFG {
    // Main Method
    public static void Main(String[] args)
        // Create a StringBuilder object
        // with a String passed as parameter
        StringBuilder str1 = 
         new StringBuilder("GeeksforGeeks");
        // printing the MaxCapacity of str1
        Console.WriteLine("Maximum Capacity of str1 is: " 
                                     + str1.MaxCapacity);
        // Create a StringBuilder object
        StringBuilder str2 = new StringBuilder();
        // printing the MaxCapacity of str2
        Console.WriteLine("Maximum Capacity of str2 is: " 
                                     + str2.MaxCapacity);



Maximum Capacity of str1 is: 2147483647
Maximum Capacity of str2 is: 2147483647


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.