最近關於使用String.Format()的question came up。我的部分答案包括使用StringBuilder.AppendLine(string.Format(...))的建議。喬恩Skeet建議這是一個不好的例子,並建議使用AppendLine和AppendFormat的組合。什麼時候使用StringBuilder.AppendLine/string.Format與StringBuilder.AppendFormat?
它發生在我身上,我從來沒有真正將自己解決爲使用這些方法的「首選」方法。我想我可能會開始使用類似下面的,但我有興趣瞭解其他人的「最佳實踐」使用方法:
sbuilder.AppendFormat("{0} line", "First").AppendLine();
sbuilder.AppendFormat("{0} line", "Second").AppendLine();
// as opposed to:
sbuilder.AppendLine(String.Format("{0} line", "First"));
sbuilder.AppendLine(String.Format("{0} line", "Second"));
也許你的代碼示例可以說明兩種情況? :) – annakata 2008-12-08 14:44:05