Remember that the previous rule is "doubling the complement."
When applying for increased capacity, the ensurecapacity () function doubles the string capacity, regardless of 3,721, and if the capacity doubled is greater than the requisition capacity, then the doubled capacity is the new capacity; If the doubled capacity is still smaller than the requisition capacity, the application capacity is the new capacity.
before:
StringBuilder buffer = new StringBuilder("Hello Kitty!"); //容量为16
buffer.EnsureCapacity(28); //请求把容量增加到28
Console.WriteLine("Length = {0}", buffer.Length);
Console.WriteLine("Capacity = {0}", buffer.Capacity);
The results are:
Now, the same program,
StringBuilder buffer = new StringBuilder("Hello Kitty!"); //容量为16
buffer.EnsureCapacity(28); //请求把容量增加到28
Console.WriteLine("Length = {0}", buffer.Length);
Console.WriteLine("Capacity = {0}", buffer.Capacity);
The results are:
How much is the application?
Has the rules changed?