You can use the string.Join
method to do something like string.Join(",", o.Number, o.Id, o.whatever, ...)
.
edit: As digEmAll said, string.Join is faster than StringBuilder. They use an external implementation for the string.Join.
Profiling code (of course run in release without debug symbols):
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
string r;
int iter = 10000;
string[] values = { "a", "b", "c", "d", "a little bit longer please", "one more time" };
sw.Restart();
for (int i = 0; i < iter; i++)
r = Program.StringJoin(",", values);
sw.Stop();
Console.WriteLine("string.Join ({0} times): {1}ms", iter, sw.ElapsedMilliseconds);
sw.Restart();
for (int i = 0; i < iter; i++)
r = Program.StringBuilderAppend(",", values);
sw.Stop();
Console.WriteLine("StringBuilder.Append ({0} times): {1}ms", iter, sw.ElapsedMilliseconds);
Console.ReadLine();
}
static string StringJoin(string seperator, params string[] values)
{
return string.Join(seperator, values);
}
static string StringBuilderAppend(string seperator, params string[] values)
{
StringBuilder builder = new StringBuilder();
builder.Append(values[0]);
for (int i = 1; i < values.Length; i++)
{
builder.Append(seperator);
builder.Append(values[i]);
}
return builder.ToString();
}
}
string.Join took 2ms on my machine and StringBuilder.Append 5ms. So there is noteworthy difference. Thanks to digAmAll for the hint.
+
symbol. It creates more objects to be garbage collected. – Wernher