0

I am reviewing some code and I see a large amount of string concatentation but they are all very small strings. Something like this:

public string BuildList()
{
    return "A" + GetCount() + "B" + TotalCount() + "C" + AMountLeft()
         + "D" + DaysLeft + "R" +  Initials() + "E";
}

I am simplifying it but in total the longest string is about 300 characters and the number of + are about 20. I am trying to figure out if its worth to convert it to StringBuilder and do something like this:

public string BuildList()
{
    var sb = new StringBuilder();
    sb.Append("A");
    sb.Append(GetCount());
    sb.Append("B");
    sb.Append(TotalCount());
    sb.Append("C");
    sb.Append(AmountLeft());
    sb.Append("D");

    // etc . . 
}

I can't see a big difference from testing but wanted to see if there is a good breakeven rule about using StringBuilder (either length of string or number of different concatenations)?

poke
  • 369,085
  • 72
  • 557
  • 602
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 1
    possible dup with: http://stackoverflow.com/questions/1612797/string-concatenation-vs-string-builder-performance – Matt Apr 12 '14 at 18:12
  • If you concat strings with `+` in a ***loop*** (such as `foreach` or `while`), you should change that to `string.Concat`, `string.Join` or similar. If the loop is too complex for that, change to a `StringBuilder`. If there is no loop statement, it is hardly a problem to use `+`. In your case there are many `+` in the very same expression, and that is compiled to a single `Concat` method call. – Jeppe Stig Nielsen Apr 12 '14 at 19:32

2 Answers2

5

For such a fixed situation with limited number of variable strings, and with a always-the-same part of text between those values, I would personally use string formatting there. That way, your intention gets a lot more clear and it’s easier to see what’s happening:

return string.Format("A{0}B{1}C{2}D{3}R{4}E",
    GetCount(), TotalCount(), AmountLeft(), DaysLeft(), Initials());

Note that string.Format is slightly slower than string.Concat (which as others said is used when you build a string using +). However, it’s unlikely that string concatenation will be a bottleneck in your application, so you should favor clarity over micro-optimization until that becomes an actual problem.

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
  • @JonSkeet Just realized that myself, no idea what was going on in my head there. Fixed :) – poke Apr 12 '14 at 18:17
  • That's a good alternative, but this answer doesn't actually answer the question. – Guffa Apr 13 '14 at 00:37
  • @Guffa The answer to [XY problems](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) is rarely the answer someone is asking for. Given that OP wants to improve code during code review, improving its readability is worth more. – poke Apr 13 '14 at 00:44
  • @poke: You are missing the point. Your answer looks like a standard answer to anything that has to do with string concatentation, it doesn't really answer anything that is in the question. – Guffa Apr 13 '14 at 19:18
4

No, in that case there is no reason to favour a StringBuilder over string concatenation.

Your first code will become a single call to String.Concat, which would be marginally more efficient that using a StringBuilder.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • We were just playing around with ildasm when we saw this question and yeah we can see that indeed just concatenating the string results in one cal to IL_0006: call string [mscorlib]System.String::Concat(string, string, string, string) – Eric Scherrer Apr 12 '14 at 18:25