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??
Use reflector to look at the ILM code and you will see exactly what is going on. Although your code logically appends new contents onto the end of the string, behind the scenes the compiler is creating ILM code that is creating a new string for each assignment.
The picture gets a little muddier if you concatenate literal strings in a single statement like this:
In this case the compiler is usually smart enough to not create all the extra strings (and thus work for the Garbage collector and will translate it for you to ILM code equivalent to:
That said, doing it on separate lines like that might not trigger that optimization.