C# – When is it better to use String.Format vs string concatenation


I've got a small piece of code that is parsing an index value to determine a cell input into Excel. It's got me thinking…

What's the difference between

xlsSheet.Write("C" + rowIndex.ToString(), null, title);


xlsSheet.Write(string.Format("C{0}", rowIndex), null, title);

Is one "better" than the other? And why?

Best Solution

My initial preference (coming from a C++ background) was for String.Format. I dropped this later on due to the following reasons:

  • String concatenation is arguably "safer". It happened to me (and I've seen it happen to several other developers) to remove a parameter, or mess up the parameter order by mistake. The compiler will not check the parameters against the format string and you end up with a runtime error (that is, if you're lucky enough not to have it in an obscure method, such as logging an error). With concatenation, removing a parameter is less error prone. You could argue the chance of error is very small, but it may happen.

- String concatenation allows for null values, String.Format does not. Writing "s1 + null + s2" does not break, it just treats the null value as String.Empty. Well, this may depend on your specific scenario - there are cases where you'd like an error instead of silently ignoring a null FirstName. However even in this situation I personally prefer checking for nulls myself and throwing specific errors instead of the standard ArgumentNullException I get from String.Format.

  • String concatenation performs better. Some of the posts above already mention this (without actually explaining why, which determined me to write this post :).

Idea is the .NET compiler is smart enough to convert this piece of code:

public static string Test(string s1, int i2, int i3, int i4, 
        string s5, string s6, float f7, float f8)
    return s1 + " " + i2 + i3 + i4 + " ddd " + s5 + s6 + f7 + f8;

to this:

public static string Test(string s1, int i2, int i3, int i4,
            string s5, string s6, float f7, float f8)
    return string.Concat(new object[] { s1, " ", i2, i3, i4, 
                    " ddd ", s5, s6, f7, f8 });

What happens under the hood of String.Concat is easy to guess (use Reflector). The objects in the array get converted to their string via ToString(). Then the total length is computed and only one string allocated (with the total length). Finally, each string is copied into the resulting string via wstrcpy in some unsafe piece of code.

Reasons String.Concat is way faster? Well, we can all have a look what String.Format is doing - you'll be surprised at the amount of code required to process the format string. On top of this (I've seen comments regarding the memory consumption), String.Format uses a StringBuilder internally. Here's how:

StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));

So for every passed argument, it reserves 8 characters. If the argument is a one-digit value, then too bad, we have some wasted space. If the argument is a custom object returning some long text on ToString(), then there might be even some reallocation needed (worst-case scenario, of course).

Compared to this, the concatenation only wastes the space of the object array (not too much, taking into account it's an array of references). There's no parsing for format specifiers and no intermediary StringBuilder. The boxing/unboxing overhead is present in both methods.

The only reason I'd go for String.Format is when localization is involved. Putting format strings in resources allows you to support different languages without messing with the code (think about scenarios where formatted values change order depending on the language, i.e. "after {0} hours and {1} minutes" may look quite different in Japanese :).

To sum up my first (and quite long) post:

  • best way (in terms of performance vs. maintainability/readability) for me is using string concatenation, without any ToString() calls
  • if you're after performance, make the ToString() calls yourself to avoid boxing (I'm somewhat biased towards readability) - same as first option in your question
  • if you're showing localized strings to the user (not the case here), String.Format() has an edge.