I know that StringBuilder is more efficient than a normal string when processing code which modifies the string value a lot because although strings act like value types, they are actually reference, which makes them immutable so every time we change it, we need to create a new reference in memory.
My question is that, why doesn’t .NET just use stringBuilder by default? There must be some disadvantages of it over just using String. Can anyone tell me what they are?
The only thing I can think of is perhaps it is a heavier object and it takes more time to instantiate so if you aren’t changing the string too much, this would override the benefits of StringBuilder
Strings are made immutable in order to be manipulated in a thread safe way. Because StringBuilder is mutable it is not thread safe. In addition you can make “copies” of a string by copying the references to the string rather than making a whole new object. Mutable objects can be changed via any of their references making this dangerous.