最近问题就来了如何使用的String.Format()。我的答案的一部分,包括建议使用StringBuilder.AppendLine(的String.Format(...))。乔恩斯基特认为这是一个坏榜样,用AppendLine和AppendFormat的组合方案。
我突然想到我从来没有真正解决自己变成了一个preferred的方式使用这些方法。我想我可能会开始使用类似下面的,但我想知道其他人作为一个最佳实践用什么:
sbuilder.AppendFormat({0}行,第一)AppendLine();
sbuilder.AppendFormat({0}行,二)AppendLine()。
//相对于:
sbuilder.AppendLine(的String.Format({0}行,第一));
sbuilder.AppendLine(的String.Format({0}行,二));
解决方案
我认为 AppendFormat
然后按 AppendLine
作为不但更加易读,也比打电话更好的性能 AppendLine(的String.Format(...))
。
后者创建了一个全新的字符串,然后批发其追加到现有的建设者。我不打算尽量去的话称:为什么使用StringBuilder的那么麻烦?但它确实显得有点反对StringBuilder的精神。
A recent question came up about using String.Format(). Part of my answer included a suggestion to use StringBuilder.AppendLine(string.Format(...)). Jon Skeet suggested this was a bad example and proposed using a combination of AppendLine and AppendFormat.
It occurred to me I've never really settled myself into a "preferred" approach for using these methods. I think I might start using something like the following but am interested to know what other people use as a "best practice":
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"));
解决方案
I view AppendFormat
followed by AppendLine
as not only more readable, but also more performant than calling AppendLine(string.Format(...))
.
The latter creates a whole new string and then appends it wholesale into the existing builder. I'm not going to go as far as saying "Why bother using StringBuilder then?" but it does seem a bit against the spirit of StringBuilder.
上一篇:如何使用PHPMailer删除裸换行符如何使用、换行符、PHPMailer
下一篇:有没有一种方法可以在默认的情况下使用PHPmaier作为sendmail_from php.ini指令?指令、情况下、方法、ini