Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

How string Behaves Like Value Type as it is reference Type

Rate me:
Please Sign up or sign in to vote.
4.59/5 (20 votes)
6 Sep 2017CPOL3 min read 25K   44   9   6
String behaves like value type as it is reference type

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.

C#
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.

Image 1

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.

C#
Square sqr1 = new square (); //declaration of an object (reference type)
Square sqr2 = new square (); //declaration of another array (reference type)
sqr1.length = 10;            //assign value to length property of square class (sqr1)
sqr2.length = 15;            //assign value to length property of square class (sqr2)
sqr2= sqr1;                  //assign sqr1 to sqr2
sqr2.length=30;              //Now in changing in sqr1 will reflect to sqr2.

Image 2

Image 3

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.

Image 4

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.

Image 5

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.

C#
string  str1;
string str2;

Both will point to separate locations in memory.

Image 6

Now assign str1 to str2.

C#
str2=str1;

Image 7

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).

Image 8

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Explanation Pin
vinothdurairaj6-Dec-18 15:49
vinothdurairaj6-Dec-18 15:49 
Questionwhy? Pin
Philip Liebscher11-Sep-17 16:23
Philip Liebscher11-Sep-17 16:23 
PraiseWell explained Pin
aeastham7-Sep-17 2:05
aeastham7-Sep-17 2:05 
QuestionGood explanation on how... Pin
lmoelleb6-Sep-17 3:45
lmoelleb6-Sep-17 3:45 
AnswerRe: Good explanation on how... Pin
israrali6-Sep-17 4:06
israrali6-Sep-17 4:06 
PraiseGood Pin
Member 132317455-Sep-17 9:35
Member 132317455-Sep-17 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.