a mutable string can be changed, and an immutable string cannot be changed.
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. a mutable string can be changed, and. an immutable string cannot be changed.
To summarise the difference, mutable objects can change their state or contents and immutable objects can't change their state or content. Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can't be changed after it is created.
Mutable is a fancy way of saying that the internal state of the object is changed/mutated. So, the simplest definition is: An object whose internal state can be changed is mutable. On the other hand, immutable doesn't allow any change in the object once it has been created.
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 new String
object. The original String
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 a StringBuilder
. An example is when you are concatenating many string fragments to form a large string:
String
concatenations, you copy O(N^2)
characters, where N
is the number of component strings.StringBuilder
you only copy O(N)
characters.And finally, for those people who assert that a StringBuilder
is not a string because it is not immutable, the Microsoft documentation describes StringBuilder
thus:
"Represents a mutable string of characters. This class cannot be inherited."
String
is immutable
i.e. strings cannot be altered. When you alter a string (by adding to it for example), you are actually creating a new string.
But StringBuilder
is not immutable (rather, it is mutable)
so if you have to alter a string many times, such as multiple concatenations, then use StringBuilder
.
An object is mutable if, once created, its state can be changed by calling various operations on it, otherwise it is immutable.
In C# (and .NET) a string is represented by class System.String. The string
keyword is an alias for this class.
The System.String class is immutable, i.e once created its state cannot be altered.
So all the operations you perform on a string like Substring
, Remove
, Replace
, concatenation using '+' operator etc will create a new string and return it.
See the following program for demonstration -
string str = "mystring";
string newString = str.Substring(2);
Console.WriteLine(newString);
Console.WriteLine(str);
This will print 'string' and 'mystring' respectively.
For the benefits of immutability and why string are immutable check Why .NET String is immutable?.
If you want to have a string which you want to modify often you can use the StringBuilder
class. Operations on a StringBuilder
instance will modify the same object.
For more advice on when to use StringBuilder
refer to When to use StringBuilder?.
All string
objects are immutable in C#. Objects of the class string
, once created, can never represent any value other than the one they were constructed with. All operations that seem to "change" a string instead produce a new one. This is inefficient with memory, but extremely useful with regard to being able to trust that a string won't change out form under you- because as long as you don't change your reference, the string being referred to will never change.
A mutable object, by contrast, has data fields that can be altered. One or more of its methods will change the contents of the object, or it has a Property that, when written into, will change the value of the object.
If you have a mutable object- the most similar one to String is StringBuffer
- then you have to make a copy of it if you want to be absolutely sure it won't change out from under you. This is why mutable objects are dangerous to use as keys into any form of Dictionary
or set- the objects themselves could change, and the data structure would have no way of knowing, leading to corrupt data that would, eventually, crash your program.
However, you can change its contents- so it's much, much more memory efficient than making a complete copy because you wanted to change a single character, or something similar.
Generally, the right thing to do is use mutable objects while you're creating something, and immutable objects once you're done. This applies to objects that have immutable forms, of course; most of the collections don't. It's often useful to provide read-only forms of collections, though, which is the equivalent of immutable, when sending the internal state of your collection to other contexts- otherwise, something could take that return value, do something to it, and corrupt your data.
Immutable :
When you do some operation on a object, it creates a new object hence state is not modifiable as in case of string.
Mutable
When you perform some operation on a object, object itself modified no new obect created as in case of StringBuilder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With