-3

Let's take this piece of code as an example:

public void func(string x1, string x2, string x3) {
    string concat = "";
    concat += x1;
    concat += x2;
    concat += x3;
}

Is the compiler smart enough to convert that to a series of StringBuilder calls?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Yahav
  • 211
  • 3
  • 4
  • Please [see](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator#addition-assignment-operator-) the documents. – Trevor Oct 21 '20 at 14:10
  • 2
    StringBuilder isn't a part of the C# language; it is a part of the .NET Framework. Developers are free to choose the approach that best suits their particular situation. – Robert Harvey Oct 21 '20 at 15:05

1 Answers1

0

No, basically. This code is compiled as:

string.Concat(string.Concat("" + x1, x2), x3);

However, if you did

string concat = x1 + x2 + x2;

it would be compiled more reasonably as a single call:

string concat = string.Concat(x1, x2, x3);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900