Are StringBuilder strings immutable?

StringBuilder has a reputation for being a faster string processing tool than just string concatenation. Regardless of whether this is true, I have to wonder about the results of StringBuilder operations and the strings they produce.

A quick walk in Reflector shows that StringBuilder.ToString () does not always return a copy, sometimes it seems that it returns an instance of the internal string. It also seems to use some internal functions to manipulate internal strings.

So what will I get if I do this?

string s = "Yo Ho Ho"; StringBuilder sb = new StringBuilder(s); string newString = sb.ToString(); sb.Append(" and a bottle of rum."); string newNewString = sb.ToString(); 

Are newString and newNewString different string instances or the same? I tried to understand this through a reflector, and I just do not quite understand everything.

How about this code?

 StringBuilder sb = new StringBuilder("Foo\n"); StringReader sr = new StringReader(sb.ToString()); string s = sr.ReadLine(); sb.Append("Bar\n"); s = sr.ReadLine(); 

Will the last statement return null or "Bar"? And if it returns one or the other, is it defined or undefined behavior? In other words, can I rely on this?

The documentation is noteworthy on this point, and I reluctantly rely on observed behavior by specification.

+6
string c #
source share
4 answers

Outside of mscorlib, any instance of System.String is unchanged, period.

StringBuilder does some interesting string manipulation inside, but at the end of the day it will not return the string to you, and then subsequently change it in a way that your code will see.

As to whether subsequent calls to StringBuilder.ToString () will return the same String instance or another String with the same value, which is implementation dependent, and you should not rely on this behavior.

+10
source share

newString and newNewString are different string instances.

Although ToString() returns the current string, it clears the current thread variable. This means that the next time you add, you will need a copy of the current line before adding.

I'm not quite sure what you are getting in your second question, but s will be empty: if the ending characters in the file are line breaks for the previous line, the line is not considered an empty line between these characters and the end of the file. The line that was read earlier has no meaning to it.

+4
source share

Are newString and newNewString different string instances or the same? I tried to understand this through a reflector, and I just did not quite understand everything.

These are different instances of string : newString is " Yo Ho Ho " and newNewString is " Yo Ho Ho and a bottle of rum. " strings are immutable, and when you call StringBuilder.ToString() , the method returns an immutable string that represents the current state.

Will the last statement return null or " Bar "? And if it returns one or the other, is it defined or undefined behavior? In other words, can I rely on him?

It will return null . StringReader working on an immutable string that you passed to it in the constructor, so it is not affected by what you do with StringBuilder .

+3
source share

The whole purpose of this class is to make the string mutable, so that's really it. I believe (but not sure) that it will return the same string as in it only if nothing has been done with this object. So after

 String s_old = "Foo"; StringBuilder sb = new StringBuilder(s_old); String s_new = sb.ToString(); 

s_old will be the same as s_new, but it will not be any other way.

I should note that for the Java compiler, automatically convert multiple string additions to operations with the StringBuilder class (or StringBuffer, which is similar, but even faster), and I would be very surprised that the .NET compiler does not do this conversion either.

+2
source share

All Articles