I’ve started using StringBuilder in preference to straight concatenation, but it seems like it’s missing a crucial method. So, I implemented it myself, as an extension:
public void Append(this StringBuilder stringBuilder, params string[] args)
{
foreach (string arg in args)
stringBuilder.Append(arg);
}
This turns the following mess:
StringBuilder sb = new StringBuilder();
...
sb.Append(SettingNode);
sb.Append(KeyAttribute);
sb.Append(setting.Name);
Into this:
sb.Append(SettingNode, KeyAttribute, setting.Name);
I could use sb.AppendFormat("{0}{1}{2}",..., but this seems even less preferred, and still harder to read. Is my extension a good method, or does it somehow undermine the benefits of StringBuilder? I’m not trying to prematurely optimize anything, as my method is more about readability than speed, but I’d also like to know I’m not shooting myself in the foot.
I see no problem with your extension. If it works for you it’s all good.
I myself prefere: