Introduction

A string is a reference data type which is used to store text. Most of the developers consider string as a value type because of its behavior like value type. It is a small but very imperative concept to understand, why string is behaving like value type. To achieve this, there is a need to understand the actual problem, reason of this situation.

Before coming to the reason, we need to understand value type and reference type.

Value Type

A data type that stores its content in the stack (a special region of memory). Value type holds the value and if you are going to assign it to another variable, the value will be copied directly and both the variables will work independently. Let’s understand with an example.

Int a=50; //declare and initialize variable a.
Int b=50; //declare and initialize variable b.
b=a; //Assign variable a to b. Here only the value of variable will be copied to b.//Now changes in variable a or b will not reflect to each other.
b=30; //Only value of b variable will change and variable a will hold 50 as its previous value.

Reference Type

A data type that stores its content in the heap (a special region of memory). In reference type variable, it holds the reference (address) of the object instead of the value assigned to that object. Whenever you assign one object to another, it will create a second copy of reference which will refer to the same location. So that in case of changing any one will reflect to the other as both are referring to the same location. Let’s understand with an example.

After having a basic understanding pertaining to value and reference type, now we can easily presume that how string should behave as it is a reference type but what we see is, it’s behaving like a value type. The answer is because string is immutable. This one line statement has some depth. So it is imperative to understand or get a deeper dive into mutable and immutable.

Immutable

In Immutable (which means Unchangeable) objects, the state of the object cannot change once it has been created. On every modification, it will not make changes in the same location but will create a new instance in memory.

Mutable

In mutable (which means changeable) objects, the state of the object can be changed after its creation. On any changes, it will change on the same location in the memory.

After having an understanding about value, reference type along with Mutable and immutable, now it’s time to find why string behaves like a value type as it is a reference type. The reason is that whenever we create a string and pass that string’s reference to another string, it copies the reference (memory address) of the referred string so that both strings will refer to the same location but when we make changes in either string, it creates a new location in memory and updates the text in that location. So the other (second) string will keep referring to the previously location. So after any change in the string, both variables will refer to separate memory locations. Let’s do it with an example.

Create two strings with the names str1 and str2.

string str1;
string str2;

Both will point to separate locations in memory.

Now assign str1 to str2.

str2=str1;

Both strings are referring to the same location in memory. Now make changes in str1. Here, the immutable concept will apply (on every change, create new location in memory).

Now we can understand that str1 now will locate to new memory (3) and str2 will still be located on the previous memory location (1) where nothing is changed. That’s how, on any modification, string does not get updated.

I like the graphical representation on what is happening behind the scenes.

I would have preferred using the word "How" instead of "Why". So "... now it's time to find out how string behaves like a reference...", as to me the "why" is related to "why did they make this choice when implementing the .net framework/languages?" - and "how" is the answer to "how did they make a reference type behave like a value type once it was decided it should behave like this".

I am not sure what you exactly means with "Resolution" as a paragraph header at the end. There is no problem that needs to be resolved (unless your goal is to answer the question "how do you make a string work like a reference type" - but I do not see this question anywhere).

You mention the StringBuilder can be used if the desired behavior is to get a "reference type string" which is correct. But it is most certainly not good practice to use a StringBuilder whenever you require to modify or append a string. If you change it to "repeatedly modify or append" you would be close.