dotnetcore/CAP · error · ArgumentException
arrayIndex
Error message
arrayIndex
What it means
CircularBuffer<T>.CopyTo throws ArgumentException("arrayIndex") when Count > array.Length - arrayIndex, i.e. the destination array is too small to hold all buffered items starting at the given offset. This is the capacity guard for the copy loop array[arrayIndex++] = item.
Solutions
- Allocate the destination with room for the offset: new T[buffer.Count + arrayIndex].
- Use array.Length - arrayIndex check at the call site before copying, or call ToArray()/ToArray(segment) style helpers.
- Re-derive Count from the buffer at copy time rather than caching a stale size.
Example fix
// before var dest = new T[buffer.Count]; buffer.CopyTo(dest, 5); // needs Count+5 slots // after var dest = new T[buffer.Count + 5]; buffer.CopyTo(dest, 5);
Defensive patterns
Strategy: validation
Validate before calling
if (array.Length - offset < buffer.Count)
array = new T[buffer.Count + offset];
buffer.CopyTo(array, offset); Try / catch
try { buffer.CopyTo(array, offset); }
catch (ArgumentException) { array = new T[buffer.Count + offset]; buffer.CopyTo(array, offset); } Prevention
- Size destinations as Count + arrayIndex, not just Count
- Re-read Count at copy time instead of caching it
- Prefer allocating a fresh array over reusing stale buffers
When it happens
Trigger: Calling CopyTo with an array whose available space (Length - arrayIndex) is less than the buffer's Count, e.g. copying into new T[n] with n < Count or using a nonzero offset in an array sized exactly to Count.
Common situations: Reusing a stale array allocated for an older smaller Count while the ring buffer wrapped and grew, or applying a non-zero arrayIndex without enlarging the array.
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
- Specified method is not supported.
- Value cannot be null. (Parameter 'array')
- Specified argument was out of the range of valid values…
- Value cannot be null. (Parameter 'topicNames')
- Value cannot be null. (Parameter 'topics')
AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14).
Data as JSON: /api/errors/df426627e66beaba.
Report an issue: GitHub.
Appendix: source
Thrown at src/DotNetCore.CAP.Dashboard/CircularBuffer.cs:164
public void Clear()
{
_firstIndex = 0;
Count = 0;
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (Count > array.Length - arrayIndex) throw new ArgumentException("arrayIndex");
// Iterate through the buffer in correct order.
foreach (var item in this)
{
array[arrayIndex++] = item;
}
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
#endregion
}View on GitHub (pinned to e52b8508e5)