I think with string.Format()
it's easier to see what exactly is the result going to be (so you don't have problems with forgotten spaces or something like that), and it's also easier to type and modify.
If you want to do very simple formatting, using the +
plus operator might be easier, but I tend to use it only when concatenating two strings, not more.
To show how string.Format()
is easier to modify, consider that you wanted to add a full stop at the end of the sentence in your example: going from string.Format("The int is {0}", i)
to string.Format("The int is {0}.", i)
is just one character. But going from "the int is " + i
to "the int is " + i + '.'
is much more.
Another advantage of string.Format()
is that it allows you to easily specify the format to use, like string.Format("The int is 0x{0:X}.", i)
. This is even more important when formatting date.
As for efficiency, string.Format()
is most likely slower that simple string concatenations. But code like this is most likely not on a hot path, so it doesn't matter. And if it does, you are probably better off with using StringBuilder
.