C# code:
string first = "A";
first += "B";
first += "C";
string second = "D" + "E" + "F";
Generated IL code:
.locals init ([0] string first,
[1] string second)
IL_0000: nop
IL_0001: ldstr "A"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldstr "B"
IL_000d: call string [mscorlib]System.String::Concat(string,
string)
IL_0012: stloc.0
IL_0013: ldloc.0
IL_0014: ldstr "C"
IL_0019: call string [mscorlib]System.String::Concat(string,
string)
IL_001e: stloc.0
IL_001f: ldstr "DEF"
IL_0024: stloc.1
IL_0025: ret
It is obvious that the in-line concatenation is a bit more efficient because it calls ldstr only once, but are there any other differences (for instance string objects created in memory?)
Thanks
Yes, precisely as you say – string objects created in memory.
Try turning the IL back into C# by hand. The code is pretty much equivalent to:
Further advantages can come down the line. As it is the following strings will be in the intern pool: “A”, “B”, “C”, “DEF”. Just what’s more advantageous here depends on a few things, but it’s likely that having the strings that are actually used in the pool is best, and “A”, “B” & “C” aren’t used except to create “ABC”. In real code though, those substrings would likely be more signficant.
Most importantly though, note that this isn’t a fair comparison between in-line vs. line by line. Try the following:
Because the strings aren’t hard-coded literals, the available optimisations are different, so the comparison between the two will differ. Yet more differences will be the case in other situations.