stride3d/stride · error · ArgumentOutOfRangeException
Invalid existing index
Error message
Invalid existing index {index} for source length {sourceLength} What it means
Thrown by CheckExistingIndexArgument when an index meant to address an EXISTING element falls outside [0, sourceLength-1]. Unlike insertion indices, an existing-element index may not equal Count. Raised from RemoveAt (and validated on construction) as ArgumentOutOfRangeException naming 'index'.
Solutions
- Validate 0 <= i < deque.Count before RemoveAt
- Re-fetch Count immediately before each removal inside loops
- Guard against an empty deque before removing by index
Example fix
// before for (int i = 0; i <= deque.Count; i++) deque.RemoveAt(i); // throws at i == Count // after for (int i = deque.Count - 1; i >= 0; i--) deque.RemoveAt(i);
Defensive patterns
Strategy: validation
Validate before calling
if (index < 0 || index >= deque.Count) throw new ArgumentOutOfRangeException(nameof(index), $"Index {index} invalid for Count {deque.Count}");
deque.RemoveAt(index); Prevention
- Remember existing-element indices max out at Count-1
- Re-fetch Count each iteration of removal loops
- Never reuse indices captured before mutations
When it happens
Trigger: Calling deque.RemoveAt(i) where i < 0 or i >= Count.
Common situations: Looping with i <= Count instead of i < Count; removing from an empty deque (any index is invalid); a stale index captured before other removals shrank the deque.
Related errors
- Invalid new index for source length
- Invalid offset
- Invalid count
- 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/9a9ccc038e395769.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:405
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>
/// 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);
}
View on GitHub (pinned to 96fad776d2)