What do you mean by "Once created a string cannot be changed" ?

Posted by Akiii under C# on 2/29/2012 | Points: 10 | Views : 1007 | Status : [Member] | Replies : 4
Hi all,
I have always approximately read everywhere that "Once created a string cannot be changed ". Well, can anyone tell me with examples, what is the problem when we declare a string like this :-

string s1 = "Akiii";
string s1 = "Lethal";

Is "s1" getting re-assigned to a newer value or is it creating a different string object s1 and assigning a different value ?


Thanks and Regards
Akiii




Responses

Posted by: Blessyjees on: 2/29/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

You can reassign value to S1, but do not repeat the declaration.

try like this
string s1 = "Akiii";
s1 = "Lethal";



Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 2/29/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi blessy,

Thank you for your reply. Is "s1" been re-assigned to a different value here ?

if it is the case then what is the statement telling us "Once created a string cannot be changed" ?

Regards
Akiii


Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aluvalavijay on: 2/29/2012 [Member] Starter | Points: 25

Up
0
Down
Hi
first you assign a value for the s1 string variable internally it will assign some memory to that value . if you reassign a value to the s1 variable then internally s1 release the memory to the garbage collection then when you assign the new value it will again create a memory for new value.
hope it will justfy your statement "Once created a string cannot be changed.

Thanks,
Vijay.Aluvala

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 2/29/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi vijay ,
thanks for your reply, but can you elaborate a little bit more ?

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response