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.
Chris s
source share