Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between capacity and maxCapacity in StringBuilder constructor?

There is a constructor in StringBuilder class (int32, int32). You see there are two parameters capacity and maxCapacity respectively.

I have tried to search the difference between capacity and maxCapacity in StringBuilder constructor. But, I didn't get anything to understand that constructor. I have found documentation on msdn. Still, I don't understand why it is required and what is the use of these parameters. Some question still in my mind that where should I use this constructor? And does it help to improve my application's performance?

like image 551
Shell Avatar asked Mar 01 '14 06:03

Shell


2 Answers

From the MSDN : StringBuilder(int,int)

Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.

So the Capcity is the size with which it has tobe started/created and MaxCapacity is the Limitation of the Stringbuilder.

Example 1: Try the following example one by one

StringBuilder str = new StringBuilder(3, 5);
str.Append("1");      //no error as Length 1 <= max limit 5         
str.Append("12");     //no error as Length 2 <= max limit 5           
str.Append("123");    //no error as Length 3 <= max limit 5   
str.Append("1234");   //no error as Length 4 <= max limit 5        
str.Append("12345");  //no error as Length 5 <= max limit 5        
str.Append("123456"); //error as Length 6 is not <= max limit 5  

Example 2: Try the following example all at once

StringBuilder str = new StringBuilder(3, 5);
str.Append("1");      //no error as str Length 1 <= max limit 5         
str.Append("12");     //no error as str Length 3 <= max limit 5           
str.Append("123");    //error as str Length 6 is not <= max limit 5   

EDIT:

Yes MaxCapacity is seldomly used.

StringBuilder(int Capacity) : with Capacity parameter it creates the StringBuilder object in memory.

as soon as user adds items and if the size exceeds its capacity limit then it allocates more memory to accomdate the exceeded characters and it keeps growing untill unless there is no problem with the memory.

StringBuilder(int Capacity,int maxCapacity) : it does the same as above one parameter Constructor but before creating/increasing its runtime memory to accomdate exceeded characters it checks for MAXCAPACITY limit ,if it exceeds the MAXCAPACITY limit then throws the Exception.


From the below commnets : As @Sriram said MaxCapacity parameter has nothing to do with memory allocation.

like image 112
Sudhakar Tillapudi Avatar answered Oct 03 '22 20:10

Sudhakar Tillapudi


It's important to understand that objects like a StringBuilder have to provide internal storage for their data. That is generally going to be an array. Usually, the size of that array will be fairly small. As you add data to the object, whenever the data grows beyond the bounds of that array, more space must be allocated. If you specify an initial capacity then the internal array will initially be created with that size. That means that there will be less need to resize the array as data is added. If you know that you will be adding at least N characters to the StringBuilder then it makes sense to specify N as the initial capacity to avoid unnecessary resizing. maxCapacity is the largest size that that internal array can be increased to.

like image 37
jmcilhinney Avatar answered Oct 03 '22 21:10

jmcilhinney