AvaloniaUI/Avalonia · error · ArgumentException
The target array is too small.
Error message
The target array is too small.
What it means
ArgumentException thrown by AvaloniaList.ICollection.CopyTo when the destination array does not have enough remaining space from the starting index to hold all Count elements of the list.
Source
Thrown at src/Avalonia.Base/Collections/AvaloniaList.cs:681
if (array.Rank != 1)
{
throw new ArgumentException("Multi-dimensional arrays are not supported.");
}
if (array.GetLowerBound(0) != 0)
{
throw new ArgumentException("Non-zero lower bounds are not supported.");
}
if (index < 0)
{
throw new ArgumentException("Invalid index.");
}
if (array.Length - index < Count)
{
throw new ArgumentException("The target array is too small.");
}
if (array is T[] tArray)
{
_inner.CopyTo(tArray, index);
}
else
{
//
// Catch the obvious case assignment will fail.
// We can't find all possible problems by doing the check though.
// For example, if the element type of the Array is derived from T,
// we can't figure out if we can successfully copy the element beforehand.
//
Type targetType = array.GetType().GetElementType()!;
Type sourceType = typeof(T);
if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
{View on GitHub (pinned to 11c5427268)
Solutions
- Allocate the destination as new T[list.Count] and use index 0.
- Ensure array.Length - index >= list.Count before calling CopyTo.
- Recompute the array size from the current Count immediately before copying.
Example fix
// before var arr = new T[5]; ((ICollection)list).CopyTo(arr, 3); // list.Count == 4 // after var arr = new T[list.Count]; ((ICollection)list).CopyTo(arr, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (array.Length - index < list.Count) throw new ArgumentException("Destination too small."); Prevention
- Size the destination as new T[list.Count] and use index 0.
- Recompute size from current Count right before copying.
- Verify array.Length - index >= Count at the call site.
When it happens
Trigger: Calling CopyTo with an index/array combination where array.Length - index < list.Count; e.g. a 5-element array starting at index 3 for a 4-item list.
Common situations: Under-allocating the destination array (using an old count), reusing a shared buffer that is too small, or an index that is not 0 leaving insufficient tail room.
Related errors
- array
- Multi-dimensional arrays are not supported.
- Non-zero lower bounds are not supported.
- Invalid index.
- Invalid array type
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/b6ee01e584dc06e2.
Report an issue: GitHub.