Copy Stringbuilder

  • Thread starter Thread starter Christof Nordiek
  • Start date Start date
C

Christof Nordiek

Hi,

in my Application i have to make a Copy of a StringBuilder,
so that, if the new old instance is changed, the you instance will not be
affected.

I could do something like
new StringBuilder(myStringBuilder.ToString())

but as i understand correct this would twice copy the content of my
StringBuilder
1. from StringBuilder to string
2. from string to StringBuilder.

Im using StingBuilder to prevent superfluous copying of large strings.

Christof
 
Christof,

What you are doing is correct. The ToString() method does not copy the
string data in the StringBuilder instance. It just references the data.

Best Regards
Johann Blake
 
Johann Blake said:
Christof,

What you are doing is correct. The ToString() method does not copy the
string data in the StringBuilder instance. It just references the data.

Best Regards
Johann Blake

Can't imagine that.
If it was so, then the content of the string Instance could be changed by
the StringBuilder.

Best Regards
Christof Nordiek
 
Christof,
My understanding is that StringBuilder.ToString does not copy the string
data, it just references the data as Johann stated.

I understand that when you go to modify the StringBuilder data after calling
it's ToString, it then copies the existing data & "starts over".

Seeing as I (and I suspect most other developers) normally "throw" the
StringBuilder away after calling its ToString, this is a good algorithm to
follow.

Hope this helps
Jay


| | > Christof,
| >
| > What you are doing is correct. The ToString() method does not copy the
| > string data in the StringBuilder instance. It just references the data.
| >
| > Best Regards
| > Johann Blake
| >
|
| Can't imagine that.
| If it was so, then the content of the string Instance could be changed by
| the StringBuilder.
|
| Best Regards
| Christof Nordiek
|
|
 
Hi Jay,
thanks for responding.

Jay B. Harlow said:
Christof,
My understanding is that StringBuilder.ToString does not copy the string
data, it just references the data as Johann stated.

I understand that when you go to modify the StringBuilder data after
calling
it's ToString, it then copies the existing data & "starts over".
....
Yes, that could be (but does someone knows it for sure?).
But that wouldn't solve my problem, since in most cases I will have to reuse
the
original as StringBuilder, so the copy will occur anyway.
 
Christof,
| Yes, that could be (but does someone knows it for sure?).
You could always use Reflector or ILDASM to look at how the StringBuilder
class is implemented.

Looking back to your original question: Have you considered creating a
wrapper around StringBuilder that has the specific behavior you're after?

Unfortunately whether ToString itself makes a copy or any method after
ToString makes a copy, you are still going to have copies of the "string"
made. As the "string" needs to go from one StringBuilder to the other...

It sounds like you really need a StringBuilder.Append(StringBuilder) method
to have a single copy done instead of 2, however I don't see one of them...

Hope this helps
Jay


| Hi Jay,
| thanks for responding.
|
| Newsbeitrag | > Christof,
| > My understanding is that StringBuilder.ToString does not copy the string
| > data, it just references the data as Johann stated.
| >
| > I understand that when you go to modify the StringBuilder data after
| > calling
| > it's ToString, it then copies the existing data & "starts over".
| > ....
| Yes, that could be (but does someone knows it for sure?).
| But that wouldn't solve my problem, since in most cases I will have to
reuse
| the
| original as StringBuilder, so the copy will occur anyway.
|
| > ....
| > | > | | > | > Christof,
| > | >
| > | > What you are doing is correct. The ToString() method does not copy
the
| > | > string data in the StringBuilder instance. It just references the
| > data.
| > | >
| > | > Best Regards
| > | > Johann Blake
| > | >
| > |
| > | Can't imagine that.
| > | If it was so, then the content of the string Instance could be changed
| > by
| > | the StringBuilder.
| > |
| > | Best Regards
| > | Christof Nordiek
| > |
| > |
| >
| >
|
|
 
Back
Top