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

  1. Check 0 <= index && index < sortedList.Count before calling RemoveAt.
  2. When removing inside a loop, iterate downward (for i = Count-1; i >= 0; i--) so shifts do not invalidate indices.
  3. Prefer Remove(key) if you have the key; it looks up the position for you and no-ops on missing keys.
  4. 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

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


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)