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
Just create an extension method.
public static StringBuilder AppendLine(this StringBuilder builder, string format, params object[] args)
{
builder.AppendFormat(format, args).AppendLine();
return builder;
}
Reasons I prefer this:
AppendLine(string.Format(...))
, as stated above..AppendLine()
part at the end (happens frequently enough).If you don't like it being called 'AppendLine,' you could change it to 'AppendFormattedLine' or whatever you want. I enjoy everything lining up with other calls to 'AppendLine' though:
var builder = new StringBuilder();
builder
.AppendLine("This is a test.")
.AppendLine("This is a {0}.", "test");
Just add one of these for each overload you use of the AppendFormat method on StringBuilder.