I read today, in c# strings are immutable, like once created they cant be changed, so how come below code works
string str="a";
str +="b";
str +="c";
str +="d";
str +="e";
console.write(str) //output: abcde
How come the value of variable changed??
String objects are immutable, but variables can be reassigned.
You created separate objects
a
ab
abc
abcd
abcde
Each of these immutable strings was assigned, in turn, to the variable str.
You can not change the contents (characters inside) a string.
Changing the variable is a different thing altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With