stride3d/stride · error · ArgumentOutOfRangeException

Invalid count

Error message

Invalid count {count}

What it means

Thrown by CheckRangeArguments when the count of a range operation is negative. Counts must be >= 0; the deque permits zero-element ranges but never negative ones. Raised as ArgumentOutOfRangeException naming 'count'.

Solutions

  1. Validate count >= 0 before the call
  2. Fix the ordering of the subtraction that produces the count
  3. Use Math.Max(0, computedCount) if a negative result should be treated as an empty range

Example fix

// before
deque.RemoveRange(offset, removedCount - keptCount); // may be negative
// after
int count = Math.Max(0, removedCount - keptCount);
deque.RemoveRange(offset, count);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
deque.RemoveRange(offset, count);

Prevention

When it happens

Trigger: Calling CopyTo/RemoveRange-backed overloads with count < 0, e.g. RemoveRange(0, -2).

Common situations: Subtracting sizes in the wrong order (wanted - removed) yielding a negative count; int underflow in length computations; copying a 'remaining' value computed after over-advancing a cursor.

Related errors


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

Appendix: source

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

    /// <summary>
    /// Checks the <paramref name="offset"/> and <paramref name="count"/> arguments for validity when applied to a source of a given length. Allows 0-element ranges, including a 0-element range at the end of the source.
    /// </summary>
    /// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
    /// <param name="offset">The index into source at which the range begins.</param>
    /// <param name="count">The number of elements in the range.</param>
    /// <exception cref="ArgumentOutOfRangeException">Either <paramref name="offset"/> or <paramref name="count"/> is less than 0.</exception>
    /// <exception cref="ArgumentException">The range [offset, offset + count) is not within the range [0, sourceLength).</exception>
    private static void CheckRangeArguments(int sourceLength, int offset, int count)
    {
        if (offset < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(offset), "Invalid offset " + offset);
        }

        if (count < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(count), "Invalid count " + count);
        }

        if (sourceLength - offset < count)
        {
            throw new ArgumentException("Invalid offset (" + offset + ") or count + (" + count + ") for source length " + sourceLength);
        }
    }

    #endregion

    /// <summary>
    /// Gets a value indicating whether this instance is empty.
    /// </summary>
    private bool IsEmpty => Count == 0;

    /// <summary>
    /// Gets a value indicating whether this instance is at full capacity.
    /// </summary>

View on GitHub (pinned to 96fad776d2)