_poisson_
✅ Why string are immutable?
The first paragraph of the Microsoft Documentation says:
A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects.https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/ But in the page "How to modify string contents", it says:
You could modify a string in a fixed block with unsafe code, but it is strongly discouraged to modify the string content after a string is created. Doing so will break things in unpredictable ways. For example, if someone interns a string that has the same content as yours, they'll get your copy and won't expect that you are modifying their string.https://learn.microsoft.com/en-us/dotnet/csharp/how-to/modify-string-contents#programmatically-build-up-string-content Ok, I can modify string but it's not recommended... But I want to know if there are another reason to have immutable string in the C# philosophy?
21 replies