diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs index 49c3f317e4d5..8acafd6d5fc4 100644 --- a/src/mscorlib/src/System/String.cs +++ b/src/mscorlib/src/System/String.cs @@ -159,27 +159,27 @@ public static String Join(String separator, IEnumerable values) { Contract.Ensures(Contract.Result() != null); Contract.EndContractBlock(); - if (separator == null) - separator = String.Empty; - - using(IEnumerator en = values.GetEnumerator()) { if (!en.MoveNext()) return String.Empty; - StringBuilder result = StringBuilderCache.Acquire(); - if (en.Current != null) { - result.Append(en.Current); + String firstValue = en.Current; + + if (!en.MoveNext()) { + // Only one value available + return firstValue ?? String.Empty; } - while (en.MoveNext()) { + // Null separator and values are handled by the StringBuilder + StringBuilder result = StringBuilderCache.Acquire(); + result.Append(firstValue); + + do { result.Append(separator); - if (en.Current != null) { - result.Append(en.Current); - } - } + result.Append(en.Current); + } while (en.MoveNext()); return StringBuilderCache.GetStringAndRelease(result); - } + } } @@ -219,13 +219,19 @@ public unsafe static String Join(String separator, String[] value, int startInde if (count == 0) { return String.Empty; } - + + if (count == 1) { + return value[startIndex] ?? String.Empty; + } + int jointLength = 0; //Figure out the total length of the strings in value int endIndex = startIndex + count - 1; for (int stringToJoinIndex = startIndex; stringToJoinIndex <= endIndex; stringToJoinIndex++) { - if (value[stringToJoinIndex] != null) { - jointLength += value[stringToJoinIndex].Length; + string currentValue = value[stringToJoinIndex]; + + if (currentValue != null) { + jointLength += currentValue.Length; } }