String and StringBuilder

  • Thread starter Thread starter Sreenivas
  • Start date Start date
Thanks for the explanation. I have a string in some code that is used to display messages in a text
box, and I keep appending new messages to it. It sounds like I should be using stringbuilder.

Jon


Göran Andersson said:
What is a string wrapper? Is that a reference to the string that stringbuilder has manipulated?

It's actually a regular String object.

The StringBuilder internally treats it as a mutable string buffer while
working on it, but when you get it from the ToString method it's just a
regular string.

The StringBuilder keeps track of the status of the string, so that if
you have used ToString to get the buffer as a string and keep making
changes the StringBuilder, it can no longer change the current buffer
(as it's used as an immutable string outside the StringBuilder), so it
has to copy the data to a new buffer.
 
Jon said:
Thanks for the explanation. I have a string in some code that is used to display messages in a text
box, and I keep appending new messages to it. It sounds like I should be using stringbuilder.

Jon

That won't help you. Once you have gotten the string out from a
StringBuilder you can't put it back in and continue changing it. If you
continue to use the StringBuilder it will copy the string to a new
buffer, so it's practically the same as creating a new StringBuilder
each time.

If you use ToString after each change, there is no practical difference
from using regular string concatenation. These pieces of code will
create the same number of string objects:

string b = String.Empty;
for (int i=0;i<1000;i++) {
b = String.Concat(b, i.ToString());
Console.WriteLine(b);
}

StringBuilder b = new StringBuilder();
for (int i=0;i<1000;i++) {
b.Append(i);
Console.WriteLine(b.ToString());
}


(I used the String.Concat method for clarity. If you use the += operator
it will call the String.Concat method to add the strings.)
 
Jon said:
Thanks for the explanation. I have a string in some code that is used to display messages in a text
box, and I keep appending new messages to it. It sounds like I should be using stringbuilder.

Jon

That won't help you. Once you have gotten the string out from a
StringBuilder you can't put it back in and continue changing it. If you
continue to use the StringBuilder it will copy the string to a new
buffer, so it's practically the same as creating a new StringBuilder
each time.

If you use ToString after each change, there is no practical difference
from using regular string concatenation. These pieces of code will
create the same number of string objects:

string b = String.Empty;
for (int i=0;i<1000;i++) {
b = String.Concat(b, i.ToString());
Console.WriteLine(b);
}

StringBuilder b = new StringBuilder();
for (int i=0;i<1000;i++) {
b.Append(i);
Console.WriteLine(b.ToString());
}


(I used the String.Concat method for clarity. If you use the += operator
it will call the String.Concat method to add the strings.)
 
Thanks for your reply Goran and sorry for my late reply (I've been on holiday).

It sounds to me then that C# can be a bit inefficient when it comes to certain types of string
manipulation, such as this one. Did MS have a good reason for making string immutable?


Göran Andersson said:
Thanks for the explanation. I have a string in some code that is used to display messages in a
text
box, and I keep appending new messages to it. It sounds like I should be using stringbuilder.

Jon

That won't help you. Once you have gotten the string out from a
StringBuilder you can't put it back in and continue changing it. If you
continue to use the StringBuilder it will copy the string to a new
buffer, so it's practically the same as creating a new StringBuilder
each time.

If you use ToString after each change, there is no practical difference
from using regular string concatenation. These pieces of code will
create the same number of string objects:

string b = String.Empty;
for (int i=0;i<1000;i++) {
b = String.Concat(b, i.ToString());
Console.WriteLine(b);
}

StringBuilder b = new StringBuilder();
for (int i=0;i<1000;i++) {
b.Append(i);
Console.WriteLine(b.ToString());
}


(I used the String.Concat method for clarity. If you use the += operator
it will call the String.Concat method to add the strings.)
 
Jon said:
Thanks for your reply Goran and sorry for my late reply (I've been on holiday).

It sounds to me then that C# can be a bit inefficient when it comes to certain types of string
manipulation, such as this one. Did MS have a good reason for making string immutable?

In some cases you could get better performance with a mutable string,
but in most cases an immutable string gives the best performance.

With immutable strings a method can safely send a string reference to
another method without worrying about the string being changed. A string
never has to be copied just to protect the original.

The cases where immutable strings gives the better performance is much
more common, so overall it gives a better performance.

As with any technique, if you don't know what the code actually does,
you can of course write something that performs really badly. With any
new technique there are some new pitfalls to look out for...
 
Thanks Peter and Goran for your replies.

Thinks are never as simple as one thinks.

Jon


"Jon" <-> wrote in message Thanks for your reply Goran and sorry for my late reply (I've been on holiday).

It sounds to me then that C# can be a bit inefficient when it comes to certain types of string
manipulation, such as this one. Did MS have a good reason for making string immutable?


Göran Andersson said:
Thanks for the explanation. I have a string in some code that is used to display messages in a
text
box, and I keep appending new messages to it. It sounds like I should be using stringbuilder.

Jon

That won't help you. Once you have gotten the string out from a
StringBuilder you can't put it back in and continue changing it. If you
continue to use the StringBuilder it will copy the string to a new
buffer, so it's practically the same as creating a new StringBuilder
each time.

If you use ToString after each change, there is no practical difference
from using regular string concatenation. These pieces of code will
create the same number of string objects:

string b = String.Empty;
for (int i=0;i<1000;i++) {
b = String.Concat(b, i.ToString());
Console.WriteLine(b);
}

StringBuilder b = new StringBuilder();
for (int i=0;i<1000;i++) {
b.Append(i);
Console.WriteLine(b.ToString());
}


(I used the String.Concat method for clarity. If you use the += operator
it will call the String.Concat method to add the strings.)
 
Back
Top