This is a little overhead creating an extra array, but I doubt it is a lot. You have to measure
If it turns out that the overhead of creating arrays of strings is significant, you can reduce it by having several overloads - one for two parameters, one for three, one for four, etc ... so that only if you achieve more the number of parameters (for example, six or seven) will need to create an array. Overloads will be as follows:
public void Append(this builder, string item1, string item2) { builder.Append(item1); builder.Append(item2); } public void Append(this builder, string item1, string item2, string item3) { builder.Append(item1); builder.Append(item2); builder.Append(item3); } public void Append(this builder, string item1, string item2, string item3, string item4) { builder.Append(item1); builder.Append(item2); builder.Append(item3); builder.Append(item4); }
And then one final overload using params like
public void Append(this builder, string item1, string item2, string item3, string item4, params string[] otherItems) { builder.Append(item1); builder.Append(item2); builder.Append(item3); builder.Append(item4); foreach (string item in otherItems) { builder.Append(item); } }
I would, of course, expect these (or just your original extension method) to be faster than using AppendFormat - for this you need to AppendFormat format string.
Note that I did not do these overloads with each other pseudo-recursively - I suspect that they were built-in, but if they were not the overhead of creating a new stack frame, etc., this could become significant. (We assume that the overhead of the array is significant if we have it this far.)
Jon Skeet Aug 13 '10 at 19:49 2010-08-13 19:49
source share