In this code I try to appendFormat
a message with a length bigger than the string builder's capacity:
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(10);
sb.AppendFormat("1234567890123"); // 13 characters
Console.WriteLine(sb.Capacity);
}
Do you know what should be the output (answer at the bottom)?
Okay, let's try to change this code and init StringBuilder
with capacity, still less than the string length, for example 12:
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(12);
sb.AppendFormat("1234567890123"); // 13 characters
Console.WriteLine(sb.Capacity);
}
So, my question is: does AppendFormat
really double
the start capacity of StringBuilder
if string couldn't be appened? If the appended string's length should be 24 characters, then the Capacity
will become 48
?
Output code: 20 & 24