Tuesday, February 19, 2019

.net - 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!";

Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...