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();
concatas it can, so for example strings that are just broken up for line break purposes can usually be optimized into a single string literal.String.ConcatStringBuildercan 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.Formatalmost all of the time for two reasons:String.Formattakes aIFormatProviderwhich is passed to anyIFormattabletypes 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
StringBuilderorString.Formatthat you specifyCultureInfo.InvariantCultureif you wanted to ensure that numbers were formatted the way you intend.Two more thing to note…
StringBuilderalso has anAppendFormatfunction which gives you the flexibility ofString.Formatwithout requiring an unnecessary second buffer.StringBuilder, make sure you don’t defeat the purpose by concatenating parameters that you pass toAppend. It’s an easy one to miss.