© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
20 replies
_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.
Strings - C# Programming Guide
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.
How to modify string contents - C# Guide
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

String : Immutable reference type
C#CC# / help
3y ago
✅ why are they considered int instead of string?
C#CC# / help
3y ago
Immutable Service Options
C#CC# / help
12mo ago
Expressing intent that method parameters are immutable for reference type arguments
C#CC# / help
3y ago