Why isn't there an StringBuilder.Append overload with StringBuilder as argument?

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

Why isn't there an StringBuilder.Append() overload with a StringBuilder as
argument?
Is there a reason that I missed?
 
I think there isn't because it would be useless. Now you can do:

....
stringBuilder2.Append(stringBuilder1.ToString());
....

I find no reason why one need such method.
 
I think there isn't because it would be useless. Now you can do:
...
stringBuilder2.Append(stringBuilder1.ToString());
...

I find no reason why one need such method.

Your "reason" would make all of the existing 19 overloads of StringBuilder
"useless".
The problem is that stringBuilder1.ToString() creates a new string which I
want to avoid.
 
How do you build the other one ? You could perhaps reuse it ie. using a
single string builder rather than creating separately two of them and then
combining them ?
--
 
StringBuilder works internally with a large string, inserting data at an
incrementing position. For example, Append(bool value) calls return
this.Append(value.ToString()), where Append(string value) does the real
appending. Append(object value) works in the same way.

So if there would be a Append(StringBuilder value), it would internally
still call Append(value.ToString()), thus making it equal to call
Append(myStringBuilder.ToString()).
 
Simon Svensson said:
StringBuilder works internally with a large string, inserting data at an
incrementing position. For example, Append(bool value) calls return
this.Append(value.ToString()), where Append(string value) does the real
appending. Append(object value) works in the same way.

So if there would be a Append(StringBuilder value), it would internally
still call Append(value.ToString()), thus making it equal to call
Append(myStringBuilder.ToString()).

Not quite equal - after calling myStringBuilder.ToString(), any appends
to myStringBuilder have to create a new string, whereas with suitable
support in StringBuilder, that wouldn't be necessary.
 
Rather conceptual than technical IMO. It was just intended to build a whole
string using a single StringBuilder ;-)

For example if you push further the use of several stringbuilders and
merging them, you are on a path that is not that different to the initial
situation for strings (you'll finally ends up with copying strings to a
memory location just to move them at some other place shortly).
 
Maybe you're right, it sounds quite logical, but I suspect there are
situations where it actually could be useful.
 
Have you checked ToString ? Unless you abuse it it should be still quite
quick anyway (this is *repeated* allocations that kills you with strings so
a single copy shouldn't be that harmfull).

You could easily compare the ToString approach and the single StringBuilder
approach and see if it makes a difference in your case...

Happy coding...
 
Sure I could do that. But Iam asking why StringBuilder doesn't support that.
There must be a technical reason. Maybe it has something to do with
threadsafety.
 
Back
Top