diff --git a/src/libraries/Common/tests/Tests/System/StringTests.cs b/src/libraries/Common/tests/Tests/System/StringTests.cs index 806c39efa529a6..64ed05a6a699b9 100644 --- a/src/libraries/Common/tests/Tests/System/StringTests.cs +++ b/src/libraries/Common/tests/Tests/System/StringTests.cs @@ -317,6 +317,8 @@ void Validate(string result) Validate(string.Concat((ReadOnlySpan)values)); Validate(string.Concat((IEnumerable)values)); Validate(string.Concat((IEnumerable)values)); // Call the generic IEnumerable-based overload + Validate(string.Concat(values.Select(s => s))); + Validate(string.Concat(new List(values))); } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMultithreadingSupported))] diff --git a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs index 4610745d86b58c..183f79158d8669 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs @@ -153,6 +153,16 @@ public static unsafe string Concat(IEnumerable values) { ArgumentNullException.ThrowIfNull(values); + if (values.GetType() == typeof(List)) // avoid accidentally bypassing a derived type's reimplementation of IEnumerable + { + return Concat(CollectionsMarshal.AsSpan((List)values)); + } + + if (values is string?[] valuesArray) + { + return Concat((ReadOnlySpan)valuesArray); + } + using (IEnumerator en = values.GetEnumerator()) { if (!en.MoveNext())