string test = "This" + "is" + "a" + "test";
>
So is the above construction the same as
this construction
string test;
test += "This";
test += "is";
test += "a";
test += "test";
No; the C# compiler does the first concatenation for you, so that is
identical to:
string test = "Thisisates t";
Without this compiler trick, it would be a single Concat, so only 1
extra string:
string test = string.Concat(" This","is","a", "test");
This single Concat call will avoid unnecessary strings - I haven't
checked, but you can imagine that it internally does something like:
StringBuilder result = new StringBuilder() ;
result.Append(" This");
result.Append(" is");
result.Append(" a");
result.Append(" test");
return result.ToString ();
(actually, I think it checks the lengths first to allocate the right
amount of space in advance - 11 chars in this case).
With the second construction, this is 4 Concat calls, and lots of
extra intermediary strings:
test = string.Concat(t est, "This");
test = string.Concat(t est, "is");
test = string.Concat(t est, "a");
test = string.Concat(t est, "test");
Marc