5

Possible Duplicate:
C# String output: format or concat?

what is the benefit of using this:

Console.WriteLine("{0}: {1}", e.Code, e.Reason);

VS. this:

Console.WriteLine(e.Code + ": " + e.Reason);

????

Community
  • 1
  • 1
capdragon
  • 14,565
  • 24
  • 107
  • 153
  • You may want to look at the disassembled code to see if there is significant performance difference. I personally prefer the first one. – fardjad Jan 25 '11 at 22:20
  • I'm continually shocked by people who find format specifiers *more readable* than simple concatenations. Don't tell me you've never mistyped those and had to go back and correct a bug because of it. Also, see [this blog entry](http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/10/c-string-compares-and-concatenations.aspx). As it turns out, simple concatenation (`+`) is best at concatenating (reasonably small sets), `StringBuilder` is best when you need to build in a loop, and `Format` is best at formatting. The performance benefit of using `Format` over `+` is **negligible at best**. – Cody Gray - on strike Jan 26 '11 at 11:13

5 Answers5

4

The reason I always use .Format() is for readability and consequently bug reduction and maintenance benefits. Of course this makes no sense if you're relatively new to code as the second example is more intuitive at first. The first appears needlessly complex.

However if you choose the second pattern then you start to have problems when you have lots of variables. This is where it is easier to appreciate the benefits of the first pattern.

e.g

Console.Writeline(var1 + " " + var2 + "-" + var3 +
 " some sort of additional text" + var4);

Note the bug: I need another space after "text" but that's not easy to see in this example.

However if I do it the other way:

Console.Writeline("{0} {1}-{2} some sort of additional text{3}", 
 var1, var2, var3, var4)

It's clearer to see what's going on. Its easier to appreciate the final result when you split the formatting from the variables that are going to be used.

If we want to think even further long term then it helps with globalisation/customisation. If we put those format strings into config we can then change the formatting or ordering of the variables without touching the code.

Quibblesome
  • 25,225
  • 10
  • 61
  • 100
3

For me, the benefits of the string.Format pendant are:

  • Improved readability
  • Better translatable

From a performance perspective, I did never do any measurements; it could well be that the concatenation is faster then the string.Format pendant.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
3

Practically, the only difference is that the first allows you to control the layout

string.Format("*{0:3}*",1); // *  1*

or control formatting:

string.Format("*{0:c}*",1); // *$1.00*

The performance of concatenation can be considered when doing lots of it in tight loops, in which case StringBuilder.Append and StringBuilder.AppendFormat are both much preferred. AppendFormat and string.Format are very close, performance-wise, so there is no need to substitute the second for the first.

0

It boils down to "When is it better to use String.Format vs string concatenation".

See this question for the answer: When is it better to use String.Format vs string concatenation?

Community
  • 1
  • 1
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
-2

As strings are immutable (cant change an existing string must create a new one each time) string format can have performance benefits as it wont create as many memory references.

result = string1 + " " + string2 + " " + string3

This creates 10 references

  • result
  • string1
  • string2
  • string3
  • " "
  • " "
  • string1 + " "
  • string1 + " " + string2
  • string1 + " " + string2 + " "
  • string1 + " " + string2 + " " + string 3

result = string.Format("{0} {1} {2}", string1, string2, string3)

This creates 5 references

  • result
  • string1
  • string2
  • string3
  • "{0} {1} {2}"
Jason
  • 220
  • 3
  • 7
  • 2
    The first simply isn't true; that is one call to string.Concat, which measures all the strings, preallocates the correct space, and copies them in. No intermediate strings. – Marc Gravell Jan 25 '11 at 22:49