dotnet/wpf · error · ArgumentException
SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength
Error message
SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength
What it means
PartialArray<T>.CopyTo throws ArgumentException wrapping Collection_CopyTo_NumberOfElementsExceedsArrayLength when the destination array has insufficient room: (array.Length - Count - arrayIndex) < 0, meaning Count elements starting at arrayIndex do not fit.
Solutions
- Allocate the destination with length >= arrayIndex + partialArray.Count.
- Check (array.Length - arrayIndex) >= Count before calling CopyTo.
- Copy only the portion that fits by adjusting ranges manually if a partial copy is intended.
Example fix
// before
partialArray.CopyTo(smallBuffer, offset);
// after
if (smallBuffer.Length - offset >= partialArray.Count)
partialArray.CopyTo(smallBuffer, offset); Defensive patterns
Strategy: validation
Validate before calling
if (array.Length - arrayIndex < coll.Count) array = new T[arrayIndex + coll.Count];
Type guard
bool HasRoomForCopy(Array dest, int arrayIndex, int count) => dest.Length - arrayIndex >= count;
Try / catch
try { coll.CopyTo(dest, arrayIndex); }
catch (ArgumentException) { dest = new T[arrayIndex + coll.Count]; coll.CopyTo(dest, arrayIndex); } Prevention
- Size destinations as arrayIndex + Count before copying.
- Recheck buffer length whenever Count may have changed.
- Prefer allocating fresh exact-size buffers in batching code.
When it happens
Trigger: Calling CopyTo(array, arrayIndex) where array.Length < Count + arrayIndex — e.g. copying a full PartialArray into a smaller buffer or at a nonzero offset.
Common situations: Paging/batching code that reuses undersized scratch buffers, or assuming Count is smaller than it actually is.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot pass multidimensional array to the CopyTo method on…
- Collection_CopyTo_NumberOfElementsExceedsArrayLength
- SR.Arg_ArrayPlusOffTooSmall
- SR.Collection_BadDestArray
- SR.Collection_BadDestArray
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3ade6f7cf53f2731.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs:147
SR.Collection_CopyTo_ArrayCannotBeMultidimensional,
nameof(array));
}
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
if (arrayIndex >= array.Length)
{
throw new ArgumentException(
SR.Format(
SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength,
"arrayIndex",
"array"),
nameof(arrayIndex));
}
if ((array.Length - Count - arrayIndex) < 0)
{
throw new ArgumentException(
SR.Format(
SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength,
"arrayIndex",
"array"));
}
// do the copying here
for (int i = 0; i < Count; i++)
{
array[arrayIndex + i] = this[i];
}
}
#endregion
#region IEnumerable<T> Members
View on GitHub (pinned to 81131a70a4)