Introduction
In this article, we are going to discuss a famous question - "Why is string immutable in C#?"
What is a String?
A string is a reference data type in C#. A string is a sequential collection of characters that is used to represent text. The value of the String object is the content of the sequential collection of System. Char objects and that value is immutable (that is, it is read-only).
Why does CLR create a new object instead of modifying the existing object?
Before going to answer this question, first, we need to know the data structure which is used to store the string in memory. As we know, the string is a collection of characters, Array data structure is used to store the string in memory in the form of the character array.
What is Array?
- An array is a data structure that is used to store the collection of elements
- Array size is defined on creation itself. We cannot modify the size of the array dynamically.
Creation of String
Now, we will try to understand what happens when CLR reads the below statement, which is written to create a string.
string testString = "Siva"; // 4 characters
- First, CLR checks the type of the data type. It is a reference type that will be stored in heap memory.
- Now, CLR needs to reserve some amount of memory to store the string value. So it checks the number of characters that exist in the string to; it is 4 (4x2 bytes = 8 bytes) characters.
- A character array will be created into a heap with a size of 8 bytes, and “Siva” will be stored in memory. Let’s assume that the memory location of this is 101.
- CLR will assign 101 as a memory reference to testString.
Modification of String
Now, we will try to understand what happens when CLR reads the below statement, which is written to modify the string.
testString = "SivaSankar"; // 9 characters
- Practically if CLR tries to fit “SivaSankar” into the same array, it cannot store the entire value because the array size that was created earlier is 8 bytes (4 characters).
- But the new value “SivaSankar” is of 9 characters which cannot fit into 8 bytes array. Even if it fits, CLR still creates a new array because of other reasons mentioned below.
- A new array will be created, and “SivaSankar” will be stored in it. Let’s assume that the memory location of this is 202.
- Here, we are creating a new object instead of modifying the existing array, which makes the string immutable.
- CLR will remove the memory reference 101 from testing, which will be collected by the garbage collector later.
- Now, CLR will assign 202 as a memory reference to testString.
- A character array will be created into a heap with a size of 8 bytes, and “SivaSankar” will be stored in memory.
Array Data structure in C# String
Since the array is used to store string values, CLR needs to create a new array each and every time the string is changed due to the array's fixed size limitations.
Security in C# String
Many parameters are represented as String in network connections, database connections, URLs, usernames/passwords, etc. If the string is immutable, these will be altered and may lead to serious issues.
Synchronization and concurrency in C# String
Making String immutable automatically makes them thread-safe, thereby solving the synchronization issues.
Caching in C# String
When Compiler optimizes your String objects, there are two objects having the same value (x="Siva", and y="Siva"), and you need only one string object (for both x and y, these two will point to the same object). We call this concept as string interning.
Class loading in C# String
A string is used as an argument for class loading. If mutable, it could result in the wrong class being loaded.
That's it. I hope you like it. Please share your valuable comments.