✅ 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?
Strings - C# Programming Guide
Learn about strings in C# programming. See information on declaring and initializing strings, the immutability of string objects, and string escape sequences.
How to modify string contents - C# Guide
Review examples of several techniques to modify existing string contents in C#, which return a new string object.
13 Replies
Sure, because it would be absolute hell to use.
Kuinox#0001
REPL Result: Success
Console Output
Compile: 456.707ms | Execution: 58.910ms | React with ❌ to remove this embed.
So, first, strings are interned in C#, so two string with the same content, may have the same reference
far better response here:
Stack Overflow
Why .NET String is immutable?
As we all know, String is immutable. What are the reasons for String being immutable and the introduction of StringBuilder class as mutable?
thinker227#5176
REPL Result: Success
Console Output
Compile: 412.382ms | Execution: 59.405ms | React with ❌ to remove this embed.
this is why
The string
"abc"
has been permanently modified
And because strings, as mentioned, are interned, the literal "abc"
always refers to the same string instance.Could there be confusion about the mutability between the type
string
and the value representation of a string? string s = "asd"; s ="dsa";
and "asd" = "dsa"
? I ask because the question is marked as beginner.Not to mention there are lots of use cases that you'd use
string
as dictionary keyI marked this question as beginner because string are basic type and I think every C# user should understand why and how it works.
Notice me if I made a mistake: string are object like reference type, not value type. I went to check the .Net source code, but the string are defined as "sequential read-only collection of Char objects". and I just want to understand why there are read-only (but not read-only with a unsafe usage).
not read-only with a unsafe usageWith
unsafe
, pretty much everything is possible, so disregard this in this context.They're designed as such to eliminate a large class of beginner mistakes that plague other languages
For example, let's say you want to use a string as a key in a dictionary (which is, of course, extremely common). You read in some data from the user, use it as a key, and then modify the string. If you're modifying the same string that is a key in the dictionary, suddenly that key is no longer reachable
Unless strings don't have value equality
But... it would be weird if
"aaa" == new string('a', 3)
returned false
(As happens in Java, iirc)Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.