doubt in the datatype tutorial

Reply Forum
Forum postsubrat kumar parichha @ 2009-07-15 08:08:37
ReplyReply to
Hi myself subrat.My doubt is in the datatype section of the c# tutorial it is being mentioned that strings are immutable ie after they have been created they are never changed.Could anybody please explain this(immutable) to me in detail(it will be much better if explained through an exmple).
Thanks in advance
Bye


Forum postWebmaster @ 2009-07-15 09:45:37
ReplyReply to
Hi,

It simply means that once a string is initialized, its value is never changed. Instead, a new string is initialized and returned. So for instance, if you define a string:

string test = "Hello, world";

And you change the value, like this:

test = "Hello, different world";

The test value is not really changed - instead, a new string is initialized, with the new value, and passed to the test variable. The same goes when you use one of the string methods to alter the value, for instance, the Remove() method:

test = test.Remove(0, 1); // Removes the first character from the string

Here, a new string is initialized, containing the value of the old one, except for the first character.

I hope this helps :)











© net-tutorials.com 2006 - 2012

MailContact net-tutorials.com