dotnet/wpf · error · ArgumentException
SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSuffi…
Error message
SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array.Length)
What it means
CopyTo throws ArgumentException (TextElementCollection_CannotCopyToArrayNotSufficientMemory) when the target array is too small: array.Length < arrayIndex + count, where count is the number of elements in the collection. The copy loop would write past the end of the array, so it is rejected before any element is copied.
Solutions
- Size the array to list.Count plus the offset: new TextElementType[list.Count + arrayIndex].
- Call list.Count right before CopyTo rather than using a stale count.
- Use list.ToArray<T>() which allocates the exact size.
- Catch ArgumentException and retry with an array of Count + arrayIndex elements.
Example fix
// before var arr = new Paragraph[oldCount]; list.CopyTo(arr, 0); // after var arr = new Paragraph[list.Count]; list.CopyTo(arr, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array.Length - arrayIndex < list.Count) { array = new TextElementType[list.Count + arrayIndex]; } Try / catch
try { list.CopyTo(arr, offset); } catch (ArgumentException) { arr = new TextElementType[list.Count + offset]; list.CopyTo(arr, offset); } Prevention
- Size buffers as Count + offset right before copying
- Re-read Count after any document mutation
- Prefer list.ToArray<T>()
When it happens
Trigger: Calling CopyTo(array, arrayIndex) where array.Length - arrayIndex is less than the collection's Count.
Common situations: Reusing a cached array after the document grew (more paragraphs added); reserving only part of a larger buffer but forgetting the offset consumes space; copying between documents of different sizes.
Related errors
- array
- Cannot pass multidimensional array to the CopyTo method on…
- SR.Argument_InvalidOffLen
- SR.Collection_BadRank
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f74d74d8bf403b1f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs:614
ArgumentNullException.ThrowIfNull(array);
Type elementType = array.GetType().GetElementType();
if (elementType == null || !elementType.IsAssignableFrom(typeof(TextElementType)))
{
throw new ArgumentException("array");
}
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
if (arrayIndex > array.Length)
{
throw new ArgumentException("arrayIndex");
}
if (array.Length < arrayIndex + count)
{
throw new ArgumentException(SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array.Length));
}
for (TextElementType element = (TextElementType)this.FirstChild; element != null; element = (TextElementType)element.NextElement)
{
array.SetValue(element, arrayIndex++);
}
}
int ICollection.Count
{
get
{
return this.Count;
}
}
bool ICollection.IsSynchronized
{View on GitHub (pinned to 81131a70a4)