When is it better to use String.Format vs string concatenation?

.net, c#, string

Solution

Before C# 6

To be honest, I think the first version is simpler - although I'd simplify it to:

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

I suspect other answers may talk about the performance hit, but to be honest it'll be minimal if present at all - and this concatenation version doesn't need to parse the format string.

Format strings are great for purposes of localisation etc, but in a case like this concatenation is simpler and works just as well.

With C# 6

String interpolation makes a lot of things simpler to read in C# 6. In this case, your second code becomes:

xlsSheet.Write($"C{rowIndex}", null, title);

which is probably the best option, IMO.

Problem

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); ``` and ``` xlsSheet.Write(string.Format("C{0}", rowIndex), null, title); ``` Is one "better" than the other? And why?

Original source

Related problems