How is this function?
/// <summary>Extension methods used by RedViewerLibrary.</summary>
internal static class Extensions
{
/// <summary>A string array extension method that gets the human readable enumerated version of the array.</summary>
/// <param name="items">The strings to act on.</param>
/// <returns>A string containing the enumerated results.</returns>
/// <example>null => ""</example>
/// <example>{} => ""</example>
/// <example>{"foo"} => "foo"</example>
/// <example>{"foo", "bar"} => "foo, and bar"</example>
/// <example>{"foo", "bar", "baz"} => "foo, bar, and baz"</example>
internal static string GetEnumerated(this string[] items)
{
if (items == null || items.Length == 0)
{
return string.Empty;
}
else if (items.Length == 1)
{
return items[0];
}
const string COMMA_SPACE = ", ";
const string AND_STRING = "and ";
var totalNamesLength = items.Aggregate(0, (oldValue, name) => name.Length + oldValue);
var numberOfCommaSpaces = items.Length - 1;
var commaSpacesLength = numberOfCommaSpaces * COMMA_SPACE.Length;
var predictedLength = totalNamesLength + commaSpacesLength + AND_STRING.Length;
var sb = new StringBuilder(predictedLength);
int loopCount = items.Length - 1;
sb.Append(items[0]);
for (int idx = 1; idx != loopCount; ++idx)
{
sb.Append(COMMA_SPACE);
sb.Append(items[idx]);
}
sb.Append(COMMA_SPACE);
sb.Append(AND_STRING);
sb.Append(items[items.Length - 1]);
return sb.ToString();
}
}
String.Join(", ", array)
. \$\endgroup\$