Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ internal static bool TryCopyTo<T>(this IEnumerable<T> sequence, T[] array, int a
Debug.Assert(array != null);
Debug.Assert(arrayIndex >= 0 && arrayIndex <= array.Length);

// IList is the GCD of what the following types implement.
if (sequence is IList<T>)
// ICollection is the GCD of what the following types implement.
if (sequence is ICollection<T>)
Comment thread
prozolic marked this conversation as resolved.
Outdated
{
Comment thread
prozolic marked this conversation as resolved.
if (sequence is List<T> list)
{
Expand All @@ -126,6 +126,18 @@ internal static bool TryCopyTo<T>(this IEnumerable<T> sequence, T[] array, int a
Array.Copy(immutable.array!, 0, array, arrayIndex, immutable.Length);
return true;
}

#if !NET
// On .NET Framework, if 'sequence' is actually a covariant array (for example, string[] used as ICollection<object>),
// its ICollection<T>.CopyTo implementation may call Array.Copy and throw an ArrayTypeMismatchException when copying into a T[].
if (sequence is Array)
{
return false;
}
Comment thread
prozolic marked this conversation as resolved.
#endif

((ICollection<T>)sequence).CopyTo(array, arrayIndex);
Comment thread
prozolic marked this conversation as resolved.
Outdated
return true;
Comment thread
prozolic marked this conversation as resolved.
Outdated
Comment thread
prozolic marked this conversation as resolved.
Outdated
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,8 @@ public static IEnumerable<object[]> InsertRangeData()
yield return new object[] { s_empty, 0, new uint[] { 1, 2, 3 } };
yield return new object[] { s_manyElements, 0, new uint[] { 4, 5, 6 } };
yield return new object[] { s_manyElements, 3, new uint[] { 4, 5, 6 } };
yield return new object[] { s_manyElements, 0, new Dictionary<int, int> { [4] = 0, [5] = 0 }.Keys };
yield return new object[] { s_manyElements, 3, new Dictionary<int, int> { [4] = 0, [5] = 0 }.Keys };
Comment thread
prozolic marked this conversation as resolved.
}

[Theory]
Expand Down
Loading