Is a StringBuilder an initialized string that contains exactly (only) enough space for this string?

I am wondering this code ...

StringBuilder sb = new StringBuilder("Please read the following messages."); 

... initializes sb with a buffer the size of the string passed to the constructor. On the one hand, this may seem most logical. On the other hand, it seems to hit the target of the StringBuilder class for one of its most common uses, namely to provide the ability to increase repeated additions. (The very first call to Append , if the answer to my question is yes, would require sb to resize.)

And again, I suppose, this could be considered similarly to the constructor for List<T> , which takes an IEnumerable<T> parameter as a parameter. Perhaps the assumption in this case is that you do not plan to add a lot, but rather about what is already there.

The only real research I did on this was to check the MSDN documentation on StringBuilder for no answer (it says the constructor initializes the instance “using the specified string” but does not indicate how the string is used).


EDIT . So this is a "concrete implementation" ... doesn’t it seem strange to someone else? I mean, the purpose of the StringBuilder class is to offer an alternative to doing a lot of string operations, creating a ton of immutable string instances along the way; therefore for efficiency . I feel that the behavior of this constructor should be specified, so that the developer can make an informed decision on how to use it regardless of the platform.

I mean that it is implemented by Microsoft in a certain way; they could easily add this to the documentation (forcing other implementations to follow suit). Just a personal source of bewilderment ...

+6
stringbuilder constructor buffer
source share
4 answers

This is an implementation detail that you need not worry about. However, using . NET reflector and looking at (string, int32, int32, int32) constructor overload (which are called by other constructors), we see that it selects a capacity that is a multiple of 16 (the next largest in size for the requested size)

Edit

Actually, this is 16 x 2 ^ n, and the value of "n" is chosen as the next largest size

+5
source share

Check the Capacity StringBuilder member.

From MSDN :

StringBuilder dynamically allocates more space as needed and increases capacity accordingly. For performance reasons, StringBuilder may allocate more memory than necessary. The amount of allocated memory depends on the implementation.

+4
source share

The constructor you linked to is probably bound to StringBuilder(String, Int32, Int32, Int32) :

 public StringBuilder( string value, int startIndex, int length, int capacity ) 

So, for a string, it will probably go through: string, 0, string.Length, string.Length. Or something similar that makes sense in the context of StringBuilder.

+2
source share

The constructor that is ultimately called is called:

 // "Please read the following messages.".Length = 35 public StringBuilder(string value, int startIndex, int length, int capacity) public StringBuilder("Please read the following messages.", 0, "Please read the following messages.".Length, 16) 

(This is nothing that other answers give, and only from the reflector)
If the capacity is less than the length of the string, which in this case:

 while (capacity < length) { capacity *= 2; if (capacity < 0) { capacity = length; break; } } 

In Mono, the StringBuilder(string val) constructor allocates the int.MaxValue capacity until it is added.

The real answer lies in a method that is ultimately called inside the CLR, where length is capacity:

 [MethodImpl(MethodImplOptions.InternalCall)] private static extern string FastAllocateString(int length); 

I cannot find the source of this in SSCLI , however the Mono version (\ mono \ metadata \ object.c) is something like this:

 mono_string_new_size (MonoDomain *domain, gint32 len) { MonoString *s; MonoVTable *vtable; size_t size = (sizeof (MonoString) + ((len + 1) * 2)); ... } 

What is the size in bytes of the MonoString object plus a time length of 2.

+1
source share

All Articles