4 ms·
Strings being immutable in .NET is a good thing, for a few reasons. e.g. string pooling (less assembly bloat), string interning (performance) and performance in
by LandR 9y ago
Strings being immutable in .NET is a good thing, for a few reasons. e.g. string pooling (less assembly bloat), string interning (performance) and performance in general.
You also get linq style benefits, e.g.
string s = "this is a string";
if(s.Substring(5).ToUpperInvariant().StartsWith("I"))
Console.WriteLine("true");
Console.WriteLine(s);
If strings weren't immutable, you'd have to copy s to avoid changing / breaking it.
These are things that I would expect a .NET developer to know. They are basic fundamentals.
- whyever 9y ago> performance in general. Really? Forcing you to copy whenever you want to modify gives you worse performance. > If strings weren't immutable, you'd have to copy s to avoid changing / breaking it. That seems like a disadvantage to me. With mutable strings, you have the option to modify it in place instead of being forced to copy.
- krzat 9y agoIdeally you have strings that can be both mutable and immutable based on context. C# is not that smart so they opted into the only other sane option.