Coder Perfect

What makes a.NET String immutable? [duplicate]


String, as we all know, is unchangeable. What are the reasons for String’s immutability and the development of the mutable StringBuilder class?

Asked by Nirajan Singh

Solution #1

Overall, there can be many benefits to being immutable for things that do not need to change as part of their function. The biggest downside is that it necessitates additional buildings, however this is sometimes exaggerated (remember, you have to do several appends before StringBuilder becomes more efficient than the equivalent series of concatenations, with their inherent construction).

It would be a disadvantage if mutability was part of an object’s purpose (who would want to be modeled by an Employee object whose salary could never ever change), though it can be useful in some cases (in many web and other stateless applications, code doing read operations is separate from code doing updates, and using different objects may be natural – I wouldn’t make an object immutable and then force that pattern, but if I already had that pattern I might make an object immutable and then force that

Copy-on-write is a compromise. A reference to a “state” class is held by the “real” class. On copy operations, state classes are shared, but when the state is changed, a new copy of the state class is made. Because std:string is more commonly used with C++ than C#, it has some, but not all, of the benefits of immutable types while staying mutable.

Answered by Jon Hanna

Solution #2

There are numerous advantages to making strings immutable. It ensures thread safety and makes strings behave as intrinsic types in a straightforward and efficient manner. It also provides further runtime economies (such as allowing effective string interning to save resource utilization) and has significant security benefits, as third-party API calls cannot edit your strings.

StringBuilder was added to solve one of immutable strings’ key drawbacks: runtime generation of immutable types produces a lot of GC strain and is intrinsically slow. This problem is solved without adding unnecessary complexity to the string class by creating an explicit, mutable class to handle it.

Answered by Reed Copsey

Solution #3

Strings aren’t truly unchangeable. They’re just unchangeable in public. It means you won’t be able to change them through their public interface. However, they are malleable on the inside.

Look at the String if you don’t trust me. Reflector is used to define concat. The last couple of lines are…

int length = str0.Length;
string dest = FastAllocateString(length + str1.Length);
FillStringChecked(dest, 0, str0);
FillStringChecked(dest, length, str1);
return dest;

As you can see, FillStringChecked modifies an empty yet allocated string returned by FastAllocateString.

FastAllocateString is an extern method, and FillStringChecked is unsafe, thus it copies the bytes with pointers.

There may be better instances, but this is the best I’ve come across thus far.

Answered by Carlos Muñoz

Solution #4

String management is a costly procedure. Repeated strings can be reused rather than re-created by keeping strings immutable.

Answered by kolosy

Solution #5

Why are string types in C# immutable?

Answered by NebuSoft

Post is based on