Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Immutability

Does string immutability work by statement, or by strings within a statement?

For example, I understand that the following code will allocate two strings on the heap.

string s = "hello ";
s += "world!";

"hello" will remain on the heap until garbage collected; and s now references "hello world!" on the heap. However, how many strings does the following line allocate on the heap...1 or 2? Also, is there a tool/way to verify the results?

string s = "goodbye " + "cruel world!";
like image 683
Aaron Daniels Avatar asked Dec 16 '08 19:12

Aaron Daniels


1 Answers

The compiler has special treatment for string concatenation, which is why the second example is only ever one string. And "interning" means that even if you run this line 20000 times there is still only 1 string.

Re testing the results... the easiest way (in this case) is probably to look in reflector:

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] string s)
    L_0000: ldstr "goodbye cruel world!"
    L_0005: stloc.0 
    L_0006: ldloc.0 
    L_0007: call void [mscorlib]System.Console::WriteLine(string)
    L_000c: ret 
}

As you can see (ldstr), the compiler has done this for you already.

like image 81
Marc Gravell Avatar answered Oct 19 '22 18:10

Marc Gravell