dotnetcore/CAP · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values…

Error message

Specified argument was out of the range of valid values. (Parameter 'index')

What it means

ArgumentOutOfRangeException thrown by the CircularBuffer indexer getter: the requested index is negative or >= Count, so WrapIndex cannot map it to a valid slot in the underlying array. The buffer is empty or smaller than the caller assumes.

Solutions

  1. Check Count (and Capacity) before indexing: if (i >= 0 && i < buffer.Count) ...
  2. Use the fact that indexing is insertion-ordered; recompute indices after Add/Take operations since the window slides
  3. Iterate with foreach (backed by GetEnumerator) instead of manual indexing
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/DotNetCore.CAP.Dashboard/CircularBuffer.cs:62 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14). Data as JSON: /api/errors/0dfd16d7dd0f6bb4. Report an issue: GitHub.

Appendix: source

Thrown at src/DotNetCore.CAP.Dashboard/CircularBuffer.cs:62

    public int Capacity => _items.Length;

    /// <summary>
    /// Whether or not the buffer is at capacity.
    /// </summary>
    public bool IsFull => Count == Capacity;

    /// <summary>
    /// Access an item in the buffer. Indexing is based off
    /// of the order items were added, rather than any
    /// internal ordering the buffer may be maintaining.
    /// </summary>
    /// <param name="index">The index of the item to access.</param>
    /// <returns>The buffered item at index <paramref name="index" />.</returns>
    public T this[int index]
    {
        get
        {
            if (!(index >= 0 && index < Count)) throw new ArgumentOutOfRangeException(nameof(index));

            return _items[WrapIndex(index)];
        }
    }

    /// <summary>
    /// Convert from a 0-based index to a buffer index which
    /// has been properly offset and wrapped.
    /// </summary>
    /// <param name="zeroBasedIndex">The index to wrap.</param>
    /// <exception cref="ArgumentOutOfRangeException">If <paramref name="zeroBasedIndex" /> is out of range.</exception>
    /// <returns>
    /// The actual index that
    /// <param ref="zeroBasedIndex" />
    /// maps to.
    /// </returns>
    private int WrapIndex(int zeroBasedIndex)
    {

View on GitHub (pinned to e52b8508e5)