5

Trying to determine if it's a better practice to use string.Format in place of concatenating strings and if so, why is this? Also, are their advantages/disadvantages to one or the other that I should be aware of?

This:

string foo = "I" + " am " + " a " + " string.";

or:

string bar = string.Format("{0} am a {1}.", "I", "string");

Obviously oversimplified examples, just wanting to be clear.

Volearix
  • 1,573
  • 3
  • 23
  • 49
  • 2
    Because the first one uses only string constants, it's evaluated at compile time. So the first one is equivalent to `string foo = "I am a string.";` – p.s.w.g Nov 27 '13 at 17:02
  • 1
    It really depends on the context. In general, I only use `String.Format()` if most of my string is static and I have a few dynamic bits. Speaking of which, the usage of string vs String is typically based on whether you're calling a function. For instance, if I'm making a new string, I call `string foo = ""`, but if I'm calling Format, I use `String.Format()` Just a note since you're wondering about 'best practices' – Stachu Nov 27 '13 at 17:05
  • That's a really broad topic. Sometimes concatenation is better and other times format is better, and often `StringBuilder` is the tool of choice. In your simple example, concatenation is best. But there is no "best" in general. It depends on the circumstance. – Jim Mischel Nov 27 '13 at 17:09
  • 1
    http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html – Tim S. Nov 27 '13 at 17:15
  • @Tim S I'm not really worried so much about the microseconds it's going to take, I'm just trying to write our coding standards (which is now going to be required due to some, let's say, "lackluster" existing code in our company). I'm more worried about any kind of possible bugs that could arise with one or the other and if anyone has general "this is better because..." advice. Very subjective, I know, but still, just curious. – Volearix Nov 27 '13 at 17:26
  • 1
    See http://stackoverflow.com/questions/3276912/c-most-readable-string-concatenation-best-practice – Tim S. Nov 27 '13 at 17:44

2 Answers2

4

The "best practice" should be the thing that makes your code the most readable and maintanable. The performance difference between concatenating strings versus using string.Format versus using a StringBuilder is so small that it's essentially irrelevant.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • For this question, the difference between StringBuilder and concatenation will be near negligible, but in situations where many strings are being concatenated, there is a definite difference. Please see the following article: http://support.microsoft.com/kb/306822 – ohiodoug Nov 27 '13 at 17:27
2

Assuming the first method was not optimized at compile time, because strings are immutable it will create many intermediate strings. It'll work from left to right so there will first be "I am ", then "I am a ", and finally "I am a string." which is stored in foo.

String.format will not make intermediate strings. To my understanding it does all the manipulation in a char[] which is then made immutable by being made a String.

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • 3
    That actually will compile to a single [`String.Concat(string,string,string,string)`](http://msdn.microsoft.com/en-us/library/0eafbze3(v=vs.110).aspx) call, which I'd guess is pretty intelligent so that it doesn't create any more strings than is needed. If you wrote it with a bunch of `+=`s, then yeah, it'll do a bunch of concatenations and (unless the JITter optimizes it) have extra strings. – Tim S. Nov 27 '13 at 17:13
  • I never knew it did that. But you and [this blog post](http://geekswithblogs.net/johnsPerfBlog/archive/2005/05/27/40777.aspx) both seem to indicate that. Thanks. – Corey Ogburn Nov 27 '13 at 17:18