0

I was trying to find out a post which should explain how exactly append function works for StringBuilder.

I got across through this answer.

Now, String is immutable. I get this.

But, StringBuilder is also initializing a new temporary character array whenever we try to append something to it.

Ajay
  • 643
  • 2
  • 10
  • 27
  • You can read the code [here](https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/text/stringbuilder.cs). Long story short: StringBuilder is backed by a char array. – ProgrammingLlama Oct 12 '17 at 06:53
  • Possible duplicate of [string is immutable and stringbuilder is mutable](https://stackoverflow.com/questions/665499/string-is-immutable-and-stringbuilder-is-mutable) – Md. Tazbir Ur Rahman Bhuiyan Oct 12 '17 at 06:55
  • they are two different objects. String is immutable. StringBuilder is not. The fact that StringBuilder starts with String doesnot necessarily mean it has to be immutable too. :) – user1506104 Oct 12 '17 at 06:55
  • Strings are inmutable, so every time you modify a string a new object is created and written to memory, even if you change only one letter. StringBuilder provides a buffer (I think by default is 64 Bytes) and you can add new strings, modify etc... and they are stored in the same block of memory until the buffer is full and a new block is created. – Isma Oct 12 '17 at 06:58
  • "StringBuilder is also initializing a new temporary character array whenever we try to append something to it" why do you think that? It uses multiple char arrays to avoid large object heap, but it does not create new one every time you append, only if there is not enough space in current array. – Evk Oct 12 '17 at 07:00

2 Answers2

2

Mutable doesn't mean that it can't create new stuff. Mutable just means that its state can change after the constructor returns.

For example, this is mutable, even though string is immutable:

class Foo {
    public string Bar { get; set; }

    public void FooMethod() {
        Bar = new string('!', 10);
    }
}

Because we can change the state of it by setting Bar or calling FooMethod:

 someFoo.FooMethod();

Yes, I am creating a new string here in the FooMethod, but that does not matter. What does matter is that Bar now has a new value! The state of someFoo changed.

We say StringBuilder is mutable because its state can change, without creating a new StringBuilder. As you have looked up, StringBuilder stores a char array. Each time you append something, that char array changes to something else, but no new StringBuilders are created. This is solid proof that StringBuilder is mutable.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • You could also mention why StringBuilder is mutable: The String object is immutable. Every time you use one of the methods in the String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The StringBuilder class can be used when you want to modify a string without creating a new object. https://learn.microsoft.com/en-us/dotnet/standard/base-types/stringbuilder – Isma Oct 12 '17 at 07:03
0

The *Builder pattern is a common pattern for creating objects, especially immutable objects, using a richer interface than simply passing in a bunch of data in the constructor. The relation between class X and a class XBuilder will usually be an immutable X and an XBuilder that can build an X. XBuilder doesn't need to be immutable, because it's not the actual data type you eventually need, it's just a temporary scaffold for constructing X.

In other words, StringBuilder isn't a mutable version of string. It's a class, a unit of functionality, that exists to help you create an immutable string. You instantiate a StringBuilder, use its various Append methods to define what should be in the final string (which doesn't, logically, exist yet), and then, when you're done, call myStringBuilder.ToString() (which, I think, should have been called BuildString()), to get the actual string you wanted.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63