AvaloniaUI/Avalonia · error · ArgumentException
Destination span is shorter than the list to be copied.
Error message
Destination span is shorter than the list to be copied.
What it means
ArgumentException thrown by PooledList<T>.CopyTo(Span<T>) when the destination span is not large enough to hold every element (`span.Length < Count`). It is a precondition check before Span.CopyTo, guaranteeing the copy cannot run out of room. Unlike the array CopyTo overload it reports the failure immediately with a clear message rather than via Array.Copy.
Source
Thrown at src/Avalonia.Base/Collections/Pooled/PooledList.cs:647
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
}
var list = new PooledList<TOutput>(_size);
for (int i = 0; i < _size; i++)
{
list._items[i] = converter(_items[i]);
}
list._size = _size;
return list;
}
/// <summary>
/// Copies this list to the given span.
/// </summary>
public void CopyTo(Span<T> span)
{
if (span.Length < Count)
throw new ArgumentException("Destination span is shorter than the list to be copied.");
Span.CopyTo(span);
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
Array.Copy(_items, 0, array, arrayIndex, _size);
}
// Copies this List into array, which must be of a
// compatible array type.
void ICollection.CopyTo(Array array, int arrayIndex)
{
_ = array ?? throw new ArgumentNullException(nameof(array));
if (array.Rank != 1)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);View on GitHub (pinned to 11c5427268)
Solutions
- Allocate the destination to exactly Count: `Span<T> buf = new T[list.Count]; list.CopyTo(buf);`.
- Use the `list.Span` accessor directly to read elements without copying at all.
- Switch to the array-based CopyTo overload if you already have a correctly-sized array.
Example fix
// before Span<T> buf = new T[list.Capacity]; // wrong: too small if Capacity grew list.CopyTo(buf); // after Span<T> buf = new T[list.Count]; list.CopyTo(buf);
Defensive patterns
Strategy: validation
Validate before calling
if (span.Length < list.Count)
throw new InvalidOperationException("Buffer too small");
list.CopyTo(span); Prevention
- Always size the destination span to list.Count, never Capacity.
- Prefer reading list.Span directly to avoid the copy entirely.
- Keep Count fresh — re-read it right before allocating the buffer.
When it happens
Trigger: Calling `list.CopyTo(span)` with a span whose Length is smaller than list.Count — e.g. a stackalloc buffer, a rented array sliced too short, or an array allocated with the wrong size.
Common situations: Sizing a destination buffer to an outdated Count; off-by-one when slicing; copying into a span obtained from a smaller pool rental; assuming Count==Capacity.
Related errors
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/4ee91d7de5963b02.
Report an issue: GitHub.