String.Format vs "string" + "string" or StringBuilder? [duplicate]
Asked Answered
S

2

20

Possible Duplicates:
Is String.Format as efficient as StringBuilder
C# String output: format or concat?

What is the performance priority and what should be the conditions to prefer each of the following:

String.Format("{0}, {1}", city, state);

or

city + ", " + state;

or

StringBuilder sb = new StringBuilder();
sb.Append(city);
sb.Append(", ");
sb.Append(state);
sb.ToString();
Saharanpur answered 28/5, 2009 at 3:24 Comment(4)
@Shimmy: Look at this #7285Fewer
blogs.msdn.com/b/ricom/archive/2004/03/12/…Visser
codinghorror.com/blog/archives/001218.htmlVisser
@Visser Jeff Atwood's post is indeed a great one! ThanksSaharanpur
M
21
  • Compiler will optimize as much string concat as it can, so for example strings that are just broken up for line break purposes can usually be optimized into a single string literal.
  • Concatenation with variables will get compiled into String.Concat
  • StringBuilder can be a lot faster if you're doing several (more than 10 or so I guess) "modifications" to a string but it carries some extra overhead because it allocates more space than you need in its buffer and resizes its internal buffer when it needs to.

I personally use String.Format almost all of the time for two reasons:

  • It's a lot easier to maintain the format string than rearranging a bunch of variables.
  • String.Format takes a IFormatProvider which is passed to any IFormattable types embedded in the string (such as numeric) so that you get appropriate numeric formatting for the specified culture and overall just more control over how values are formatted.

For example, since some cultures use a comma as a decimal point you would want to ensure with either StringBuilder or String.Format that you specify CultureInfo.InvariantCulture if you wanted to ensure that numbers were formatted the way you intend.

Two more thing to note...

  • StringBuilder also has an AppendFormat function which gives you the flexibility of String.Format without requiring an unnecessary second buffer.
  • When using StringBuilder, make sure you don't defeat the purpose by concatenating parameters that you pass to Append. It's an easy one to miss.
Misquote answered 28/5, 2009 at 3:39 Comment(0)
V
1

There is no relevant diference. But assuming that String.Format internally uses a StringBuilder (you can see that with the Reflector tool), using directly StringBuilder.Append should be faster.

EDIT: Of course that use "+" operator is the worst option since it creates a new string instance for each string that you are concatenating.

Vichy answered 28/5, 2009 at 3:36 Comment(3)
wrong, according to the link i posted bellow it seems than + is the best waySaharanpur
@shimmy, There is no StringBuilder in that link. Also, in that test, you only concatenate 3 Strings, the number of strings affects the performance a lot.Hernando
The edit is wrong. The C# compiler will assemble the strings or objects into an array and then invoke String.Concat(). I would expect String.Concat(string[]) to be the most efficient because the implementation can allocate the string to exactly the correct length.Goglet

© 2022 - 2024 — McMap. All rights reserved.