TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

must be an index in the list

Error message

{nameof(position)} must be an index in the list

What it means

After walking position nodes from Head, GetAt throws ArgumentOutOfRangeException named 'position' if the traversal lands on null, meaning position pointed past the last node. This is the tail-end guard for indices that passed the initial range check only because the list state changed or the walk ended on a null Next chain.

Solutions

  1. Validate position < list.Count immediately before the call and avoid sharing the list across threads without synchronization.
  2. Catch ArgumentOutOfRangeException around GetAt and handle the missing-node case.
  3. Re-fetch Count after any mutation rather than caching it.

Example fix

// before
var node = list.GetAt(i); // i captured before list shrank
// after
if (i < list.Count)
    var node = list.GetAt(i);
else
    // handle missing index
Defensive patterns

Strategy: validation

Validate before calling

if (position >= 0 && position < list.Count)
    var node = list.GetAt(position);

Try / catch

try { var node = list.GetAt(position); }
catch (ArgumentOutOfRangeException) { /* position no longer valid */ }

Prevention

When it happens

Trigger: Calling GetAt with a position that walks past the end of the chain, e.g. GetAt(Count) or on a list whose node links were corrupted/mutated concurrently so current becomes null before i reaches position.

Common situations: Race conditions where another thread removes nodes mid-traversal, or calling GetAt between list mutations that leave Count stale relative to the node chain.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/fd97852c9bb81efd. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs:216

    ///     Looks for a node in the list that contains the value of the parameter.
    /// </summary>
    /// <param name="position"> Position in the list.</param>
    /// <returns>The node in the list the has the paramater as a value or null if not found.</returns>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when position is negative or out range of the list.</exception>
    public DoublyLinkedListNode<T> GetAt(int position)
    {
        if (position < 0 || position >= Count)
        {
            throw new ArgumentOutOfRangeException($"Max count is {Count}");
        }

        var current = Head;
        for (var i = 0; i < position; i++)
        {
            current = current!.Next;
        }

        return current ?? throw new ArgumentOutOfRangeException($"{nameof(position)} must be an index in the list");
    }

    /// <summary>
    ///     Removes the Head and replaces it with the second node in the list.
    /// </summary>
    public void RemoveHead()
    {
        if (Head is null)
        {
            throw new InvalidOperationException();
        }

        Head = Head.Next;
        if (Head is null)
        {
            Tail = null;
            Count = 0;
            return;

View on GitHub (pinned to 96e2905cab)