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
- Validate count >= 0 before the call
- Fix the ordering of the subtraction that produces the count
- 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
- Validate count >= 0 before range calls
- Double-check subtraction order when computing counts
- Clamp computed counts with Math.Max(0, n) where an empty range is acceptable
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
- Invalid new index for source length
- Invalid existing index
- Invalid offset
- Invalid offset ( ) or count + ( ) for source length
- Capacity cannot be set to a value less than Count
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)