87

Looking at the string class metadata, I only see the operators == and != overloaded. So how is it able to perform concatenation for the '+' operator?

Edit:

Some interesting notes from Eric Lippert on string concatenation:

Part 1

Part 2

There is also a super article from Joel referred in part 2 (http://www.joelonsoftware.com/articles/fog0000000319.html)

NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32

1 Answers1

145

It doesn't - the C# compiler does :)

So this code:

string x = "hello";
string y = "there";
string z = "chaps";
string all = x + y + z;

actually gets compiled as:

string x = "hello";
string y = "there";
string z = "chaps";
string all = string.Concat(x, y, z);

(Gah - intervening edit removed other bits accidentally.)

The benefit of the C# compiler noticing that there are multiple string concatenations here is that you don't end up creating an intermediate string of x + y which then needs to be copied again as part of the concatenation of (x + y) and z. Instead, we get it all done in one go.

EDIT: Note that the compiler can't do anything if you concatenate in a loop. For example, this code:

string x = "";
foreach (string y in strings)
{
    x += y;
}

just ends up as equivalent to:

string x = "";
foreach (string y in strings)
{
    x = string.Concat(x, y);
}

... so this does generate a lot of garbage, and it's why you should use a StringBuilder for such cases. I have an article going into more details about the two which will hopefully answer further questions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 8
    Good answer; just a follow-up question: if you're concatenating strings inside a loop, how does the compiler work the .Concat operator when it can't know in advance what's going to be in the loop. – frenchie Apr 26 '12 at 20:49
  • Ah ok! And that's why it's better to use the StringBuilder for concatenations? – frenchie Apr 26 '12 at 20:52
  • 4
    @frenchie: That's why it's better to use StringBuilder for *repeated* concatenation... but why it's not worth it for concatenation *of a fixed number of values*. – Jon Skeet Apr 26 '12 at 20:53
  • @Jon-Skeet: thanks for the quick clarification...i should have checked in IL as well..and this is how int adds as well? by calling the Add method? – NoviceProgrammer Apr 26 '12 at 20:57
  • StringBuilder is the way to go for string concatenation. + operator would copy the entire string to another new variable and cause and overload – Enggr Apr 27 '12 at 03:16
  • @Enggr: When looping, yes. *Not* when concatenating multiple expressions in a single statement though, as per my first example. Note that using `StringBuilder` still requires the whole string to be copied. – Jon Skeet Apr 27 '12 at 05:18
  • @NoviceProgrammer: No, `int` uses built-in IL instructions. But `decimal` would use the `op_Add` operator implementation, for example. – Jon Skeet Apr 27 '12 at 05:19
  • 2
    But note that nowadays the `string.Concat` and `string.Join` methods have many overloads, including some taking in an `IEnumerable<>` instance. So they are more convenient to use in most cases than to use a `StringBuilder` (I don't know if these methods use a `StringBuilder` internally, I just say that they are more convenient in most cases). – Jeppe Stig Nielsen Jun 24 '13 at 21:55