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
Thanks in advance
Bye
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
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
