【发布时间】:2008-12-08 14:32:18
【问题描述】:
最近的question came up 关于使用 String.Format()。我的部分答案包括使用 StringBuilder.AppendLine(string.Format(...)) 的建议。 Jon Skeet 认为这是一个不好的例子,并建议使用 AppendLine 和 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"));
【问题讨论】:
-
也许您的代码示例可以说明这两种情况? :)
标签: c# .net formatting stringbuilder