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)?

2
  • 1
    possible dup with: stackoverflow.com/questions/1612797/… Commented Apr 12, 2014 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. Commented Apr 12, 2014 at 19:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@JonSkeet Just realized that myself, no idea what was going on in my head there. Fixed :)
That's a good alternative, but this answer doesn't actually answer the question.
@Guffa The answer to XY problems 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: 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.
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.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.