C
C#2y ago
_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?
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
Kuinox
Kuinox2y ago
Sure, because it would be absolute hell to use.
MODiX
MODiX2y ago
Kuinox#0001
REPL Result: Success
Console.WriteLine("Hello, World");
Console.WriteLine("Hello, World");
Console Output
Hello, World
Hello, World
Compile: 456.707ms | Execution: 58.910ms | React with ❌ to remove this embed.
Kuinox
Kuinox2y ago
So, first, strings are interned in C#, so two string with the same content, may have the same reference far better response here:
Kuinox
Kuinox2y ago
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?
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
static unsafe void F()
{
Console.WriteLine("abc");
fixed (char* c = "abc")
{
c[0] = 'd';
c[1] = 'e';
c[2] = 'f';
}
Console.WriteLine("abc");
}
F();
static unsafe void F()
{
Console.WriteLine("abc");
fixed (char* c = "abc")
{
c[0] = 'd';
c[1] = 'e';
c[2] = 'f';
}
Console.WriteLine("abc");
}
F();
Console Output
abc
def
abc
def
Compile: 412.382ms | Execution: 59.405ms | React with ❌ to remove this embed.
Thinker
Thinker2y ago
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.
Fright XO
Fright XO2y ago
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.
cathei
cathei2y ago
Not to mention there are lots of use cases that you'd use string as dictionary key
_poisson_
_poisson_OP2y ago
I 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).
Thinker
Thinker2y ago
not read-only with a unsafe usage
With unsafe, pretty much everything is possible, so disregard this in this context.
333fred
333fred2y ago
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)
cause77
cause772y ago
static unsafe void F()
{
Console.WriteLine(new String("abc"));
fixed (char* c = "abc")
{
c[0] = 'd';
c[1] = 'e';
c[2] = 'f';
}
Console.WriteLine("abc");
}
F();
static unsafe void F()
{
Console.WriteLine(new String("abc"));
fixed (char* c = "abc")
{
c[0] = 'd';
c[1] = 'e';
c[2] = 'f';
}
Console.WriteLine("abc");
}
F();
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server