If you concatenate multiple strings in one operation with String.Concat
, .net will add together their lengths, create a new string object of suitable size, and copy all of the characters from the source strings to the new one. If you are doing four or fewer strings in a String.Concat
call, one new object will be created (the new string); no "garbage" will be created unless one of the old strings is abandoned and thus becomes garbage. If you concatenate five or more strings, a new temporary String[]
will be created to hold the parameters, but it will be small and short-lived (and thus relatively harmless), regardless of the length of the strings, unless you're passing many thousands of parameters.
Additionally, one can concatenate any number of constant strings using the +
operator in C#, or the +
or &
operators in vb.net and the compiler will turn them into one larger constant string. I'm not sure what rules dictate the extent to which the C# or vb.net compiler will consolidate strings in an expression which contains multiple constant and non-constant strings (especially if parentheses are involved); using a String.Concat
call with as many parameters as needed, but using +
to consolidate constant strings within a parameter, should yield optimal code in any case.