stride3d/stride · error · ArgumentOutOfRangeException

Invalid new index for source length

Error message

Invalid new index {index} for source length {sourceLength}

What it means

Thrown by the deque's private CheckNewIndexArgument helper when an insertion index is outside the valid insertion range [0, sourceLength]. Insertion positions may equal Count (insert at end) but never exceed it or go negative. It is raised from Insert and InsertRange as ArgumentOutOfRangeException naming the 'index' parameter.

Solutions

  1. Clamp or validate the insertion index to 0..deque.Count before calling Insert/InsertRange
  2. Check the deque is not empty when reusing a stored index (an index valid before a RemoveAt may now exceed Count)
  3. Use AddToAddBack/AddToFront if you actually meant append/prepend, which need no index

Example fix

// before
deque.Insert(deque.Count + 1, item); // throws
// after
deque.Insert(deque.Count, item); // insert at end is legal
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index > deque.Count) throw new ArgumentOutOfRangeException(nameof(index), $"Insert index {index} out of range for Count {deque.Count}");
deque.Insert(index, item);

Prevention

When it happens

Trigger: Calling deque.Insert(i, item) with i < 0 or i > Count, or deque.InsertRange(i, collection) with an out-of-range i.

Common situations: Off-by-one when inserting after the last element (using Count+1 instead of Count); computing the index from an empty deque (Count=0) where only index 0 is valid; passing an index from a differently-sized collection.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/c3bf885dac139233. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:391

    bool System.Collections.ICollection.IsSynchronized => false;

    object System.Collections.ICollection.SyncRoot => this;

    #endregion

    #region GenericListHelpers

    /// <summary>
    /// Checks the <paramref name="index"/> argument to see if it refers to a valid insertion point in a source of a given length.
    /// </summary>
    /// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
    /// <param name="index">The index into the source.</param>
    /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index to an insertion point for the source.</exception>
    private static void CheckNewIndexArgument(int sourceLength, int index)
    {
        if (index < 0 || index > sourceLength)
        {
            throw new ArgumentOutOfRangeException(nameof(index), "Invalid new index " + index + " for source length " + sourceLength);
        }
    }

    /// <summary>
    /// Checks the <paramref name="index"/> argument to see if it refers to an existing element in a source of a given length.
    /// </summary>
    /// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
    /// <param name="index">The index into the source.</param>
    /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index to an existing element for the source.</exception>
    private static void CheckExistingIndexArgument(int sourceLength, int index)
    {
        if (index < 0 || index >= sourceLength)
        {
            throw new ArgumentOutOfRangeException(nameof(index), "Invalid existing index " + index + " for source length " + sourceLength);
        }
    }

    /// <summary>

View on GitHub (pinned to 96fad776d2)