To the first bit, "yes, basically", up to
Which basically leaving "Hello" and "Hello My " weakly referenced and can be GC-ed.
No, they aren't referenced at all any more.
Edit: Henk correctly notes that "Hello"
and " My "
, however, are interned (due to ldstr
) - so will not be collected. The combined "Hello My "
, however, is not interned and will be collectable.
For the second example: because those are all constants in a single expression, the compiler combines the constant at compile time; that represents a single interned string "Hello My World"
, which you can see in the IL.
For example:
static void Main()
{
string s = "Hello";
s += " My ";
s += " World";
System.Console.WriteLine(s);
s = "Hello" + " My " + " World";
System.Console.WriteLine(s);
}
compiles to:
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 8
L_0000: ldstr "Hello"
L_0005: ldstr " My "
L_000a: call string [mscorlib]System.String::Concat(string, string)
L_000f: ldstr " World"
L_0014: call string [mscorlib]System.String::Concat(string, string)
L_0019: call void [mscorlib]System.Console::WriteLine(string)
L_001e: ldstr "Hello My World"
L_0023: call void [mscorlib]System.Console::WriteLine(string)
L_0028: ret
}
Here you can clearly see both the Concat
s for the first example, and the single pre-combined string for the second.