stride3d/stride · error · ArgumentOutOfRangeException
index out of range
Error message
index out of range
What it means
RemoveAt(int index) validates that the supplied index falls within [0, Count). If not (including on an empty list), it throws ArgumentOutOfRangeException with the message "index out of range". SortedList exposes positional removal because it keeps keys sorted in its internal table.
Solutions
- Check 0 <= index && index < sortedList.Count before calling RemoveAt.
- When removing inside a loop, iterate downward (for i = Count-1; i >= 0; i--) so shifts do not invalidate indices.
- Prefer Remove(key) if you have the key; it looks up the position for you and no-ops on missing keys.
- Use IndexOfKey(key) and check the returned index before RemoveAt.
Example fix
// before
for (int i = 0; i < list.Count; i++)
if (predicate(list.Values[i])) list.RemoveAt(i);
// after
for (int i = list.Count - 1; i >= 0; i--)
if (predicate(list.Values[i])) list.RemoveAt(i); Defensive patterns
Strategy: validation
Validate before calling
if (index < 0 || index >= sortedList.Count)
return false; // or throw your own descriptive error
sortedList.RemoveAt(index); Type guard
static bool IsValidIndex(System.Collections.Generic.SortedList<TKey,TValue> list, int i) =>
(uint)i < (uint)list.Count; Try / catch
try { sortedList.RemoveAt(index); }
catch (ArgumentOutOfRangeException ex) when (ex.Message == "index out of range")
{ /* index was stale; refetch Count or skip */ } Prevention
- Iterate backwards when removing multiple items by index.
- Never cache indices across mutations of the list.
- Check Count for empty lists before RemoveAt(0).
- Prefer Remove(key) or IndexOfKey + guard over raw positional removal.
When it happens
Trigger: RemoveAt(i) where i < 0 or i >= sortedList.Count; RemoveAt on an empty list (RemoveAt(0)); using a stale index captured before another element was removed.
Common situations: Looping RemoveAt(i) upward while removing matching items (shifts indices); Remove(key) internally calling RemoveAt(IndexOfKey(key)) when the key is absent and callers bypass checks; UI/list synchronization using out-of-date indexes.
Related errors
- element already exists
- No Collection item identifier associated to the given…
- An item has been added to a collection that does not have a…
- Two elements of the collection have the same id
- An id is both marked as deleted and associated to a key of…
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/b5253445cd62dda3.
Report an issue: GitHub.
Appendix: source
Thrown at sources/core/Stride.Core/Collections/SortedList.cs:411
{
var table = this.table;
var cnt = Count;
if (index >= 0 && index < cnt)
{
if (index != cnt - 1)
{
Array.Copy(table, index + 1, table, index, cnt - 1 - index);
}
else
{
table[index] = default(KeyValuePair<TKey, TValue>);
}
--inUse;
++modificationCount;
}
else
{
throw new ArgumentOutOfRangeException("index out of range");
}
}
public int IndexOfKey(TKey key)
{
ArgumentNullException.ThrowIfNull(key);
var indx = 0;
try
{
indx = Find(key);
}
catch (Exception)
{
throw new InvalidOperationException();
}
return (indx | (indx >> 31));View on GitHub (pinned to 96fad776d2)