开发者

When should I use String.Format or String.Concat instead of the concatenation operator?

In C# it is possible to concatenate strings in several different ways:

Using the concatenation operator:

var newString = "The answer is '" + value + "'.";

Using String.Format:

var newString = String.Format("The answer is '{0}'.", value);

Using String.Concat:

var newStri开发者_高级运维ng = String.Concat("The answer is '", value, "'.");

What are the advantages / disadvantages of each of these methods? When should I prefer one over the others?

The question arises because of a debate between developers. One never uses String.Format for concatenation - he argues that this is for formatting strings, not for concatenation, and that is is always unreadable because the items in the string are expressed in the wrong order. The other frequently uses String.Format for concatenation, because he thinks it makes the code easier to read, especially where there are several sets of quotes involved. Both these developers also use the concatenation operator and String.Builder, too.


Concerning speed it almost always doesn't matter.

var answer = "Use what makes " + "the code most easy " + "to read";


I usually use string.Format when I've chaining together more than 2 or 3 values, as it makes it easier to see what the final result would look like. Concatenating the strings is slow, as you need to create a new string object for each operation.

If you need to join more than 5 strings, use StringBuilder as it would be much faster.


Performance considerations are often the driver behind this decision. See this article by Ayende.


I normally go for readability, and would tend towards using Format. Most code is written once and read multiple times, so making sure the reader can quickly understand what's beening stated is more important (to me).


It is curious, but String.Format internally use StringBuilder.AppendFormat(). For example, String.Format code is looking like:

public static string Format(IFormatProvider provider, string format, params object[] args)
{
  if (format == null || args == null)
   throw new ArgumentNullException((format == null ? "format" : "args"));

  StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
  builder.AppendFormat(provider, format, args);
  return builder.ToString();
}

More about this you can find here. So, why we haven't mentioned here about StringBuilder.AppendFormat()!

Regarding to main point of question:

The key is to pick the best tool for the job. What do I mean? Consider these awesome words of wisdom:

* Concatenate (+) is best at concatenating. 
* StringBuilder is best when you need to building.
* Format is best at formatting.


It's not recommend to store string in code so if you will decide to extract your strings from code then with String.Format it would be easier to do


This is an article on memory usage for various concatenation methods and compiler optimizations used to generate the IL. Concatenation methods and optimization issue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜