I'm wondering if this code ...
StringBuilder sb = new StringBuilder("Please read the following messages.");
... initializes sb
with a buffer exactly as large as the string passed to the constructor. On the one hand, this would seem the most logical thing. On the other hand, it seems to kind of defeat the purpose of the StringBuilder
class for one of its most common uses, which is to provide mutability to make repeated appends more efficient. (The very first call to Append
, if the answer to my question is "yes", would require sb
to resize itself.)
Then again, I suppose one could view this as analogous to the constructor for List<T>
that takes an IEnumerable<T>
as a parameter. Maybe the assumption in this case is that you're not planning on appending a lot, but rather on manipulating what's already there.
The only real research I've done on this was to check the MSDN documentation on StringBuilder, which didn't provide an answer (it says the constructor initializes the instance "using the specified string," but doesn't indicate how the string is used).
EDIT: So it's "implementation-specific"... does this not seem weird to anyone else? I mean, the purpose of the StringBuilder
class is to offer an alternative to performing a lot of operations on a string
, creating a ton of immutable string
instances along the way; therefore, it's for efficiency. I feel like the behavior of this constructor ought the be specified, so that the developer can make an informed decision how to use it regardless of platform.
I mean, it is implemented by Microsoft a certain way; they could easily have put that in the documentation (forcing other implementations to follow suit). Just a personal source of puzzlement...