Click here to Skip to main content
15,887,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the meaning of string is immutable , i read about this topic and i understand that any new operation on that string will create a new object ? Right ??

- -
So if it is right , what is the meaning by below code ?

C#
string x="Hello";
x="good morning";


what the above code meaning ?
is x in the first line and x is the second line are the same ? or they are two different object ?
please i need to understand , so appreciate any demonstration with example , thanks
Posted

Strings are immutable.
This means that when you add something to this string or remove a character, a new string is created.
The existing string value reference remains and is not destroyed.
string a = "1";
string b = "2";
string a = a + b;

This code creates a new string in memory called "12" and assigns it to a. "1" still remains and will be collected as and when appropriate.
 
Share this answer
 
Comments
Peter Leow 9-Mar-14 4:22am    
clear and concise, 5ed.
Abhinav S 9-Mar-14 6:01am    
Thank you.
Hercal 9-Mar-14 4:24am    
so they use a string builder which is mutable because of this issue , right ?
Hercal 9-Mar-14 6:14am    
string a="Hello"; a="Hello World";............... [a] is a reference saved in [ stack] it point to an object has value of "hello" which saved in memory [ heap ] then same reference [a] now point to another object with another value saved in heap " hello world" ......is this concept right or wrong ????=============if it is wrong , please clarify and if my concept is right then i have one more question ,....what about the old object ["hello"] when it will be destroyed ?




agent_kruger 9-Mar-14 4:58am    
sir, nice explanation +5 vote. But i want to ask how to get the "1" back which was stored previously.
Quote:
String is immutable means that you cannot change the object itself, but you can change the reference ofcourse

For example,
C#
string x="Hello";
x+="good morning";
Console.WriteLine(x);  // will print, Hello good morning


-KR
 
Share this answer
 
v2
Comments
Hercal 9-Mar-14 4:30am    
ok nice ,now it is clear to me but what about the old object ? when it will be removed from memory ?
Krunal Rohit 9-Mar-14 4:33am    
old object reference would be changed by new reference. You can judge it through the output.

-KR
Richard MacCutchan 9-Mar-14 4:38am    
No it won't, it will only print "good morning"
Krunal Rohit 9-Mar-14 4:45am    
Ans is updated now..
Typo mistake.

-KR
String is immutable in nature means we can't reassign the the value to the same object.
When we will reassign the value to a variable like

string a="Hello";
a="Hello World";

In the above example first it is creating a object and assigning the "Hello" value. But when reassigning then it keeps the reference of "a" in memory and destroy it and create new object with value "Hello world".
 
Share this answer
 
Comments
agent_kruger 9-Mar-14 5:01am    
1 vote, because it has already been answered correctly. Please don't answer the question which is already being answered and if still answering provide new points.
Sorry sir, if i am rude.
Hercal 9-Mar-14 6:12am    
string a="Hello"; a="Hello World";............... [a] is a reference saved in [ stack] it point to an object has value of "hello" which saved in memory [ heap ] then same reference [a] now point to another object with another value saved in heap " hello world" ......is this concept right or wrong ????=============if it is wrong , please clarify and if my concept is right then i have one more question ,....what about the old object ["hello"] when it will be destroyed ? and is there an availability to retrieve it back.??

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900