What is the difference between a mutable and immutable string in C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Strings are mutable because .NET use string pool behind the scene. It means :
Both name and name2 are referring to same memory location from string pool. Now suppose you want to change name2 to :
It will look in to string pool for the string "My Loving Country", if found you will get the reference of it other wise new string "My Loving Country" will be created in string pool and name2 will get reference of it. But it this whole process "My Country" was not changed because other variable like name is still using it. And that is the reason why string are IMMUTABLE.
StringBuilder works in different manner and don't use string pool. When we create any instance of StringBuilder :
It allocate memory chunk of size 500 bytes for this instance and all operation just modify this memory location and this memory not shared with any other object. And that is the reason why StringBuilder is MUTABLE.
I hope it will help.
Mutable and immutable are English words meaning "can change" and "cannot change" respectively. The meaning of the words is the same in the IT context; i.e.
The meanings of these words are the same in C# / .NET as in other programming languages / environments, though (obviously) the names of the types may differ, as may other details.
For the record:
String
is the standard C# / .Net immutable string typeStringBuilder
is the standard C# / .Net mutable string typeTo "effect a change" on a string represented as a C#
String
, you actually create a newString
object. The originalString
is not changed ... because it is unchangeable.In most cases it is better to use
String
because it is easier reason about them; e.g. you don't need to consider the possibility that some other thread might "change my string". However, when you need to construct or modify a string using a sequence of operations, it may be more efficient to use aStringBuilder
.And finally, for those people who assert that a
StringBuilder
is not a string because it is not immutable, the Microsoft documentation describesStringBuilder
thus:To clarify there is no such thing as a mutable string in C# (or .NET in general). Other langues support mutable strings (string which can change) but the .NET framework does not.
So the correct answer to your question is ALL string are immutable in C#.
string has a specific meaning. "string" lowercase keyword is merely a shortcut for an object instantiated from System.String class. All objects created from string class are ALWAYS immutable.
If you want a mutable representation of text then you need to use another class like StringBuilder. StringBuilder allows you to iteratively build a collection of 'words' and then convert that to a string (once again immutable).
None, actually. The String class is mutable.
This kind of logic is done all the time in the String and StringBuilder classes, actually. They just allocate a new string each time you call Concat, Substring, etc. and use pointer arithmetic to copy over to the new string. Strings just don't mutate themselves, hence why they are considered "immutable".
By the way, do not attempt this with string literals or you will badly mess up your program:
This is because string literals are interned in the desktop .NET Framework; in other words,
bar
andbaz
point to the exact same string, so mutating one will mutate the other. This is all fine and dandy though if you're using an unmanaged platform like WinRT, which lacks string interning.String in C# is immutable. If you concatenate it with any string, you are actually making a new string, that is new string object ! But StringBuilder creates mutable string.
Here is the example for Immutable string and Mutable string builder