TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

Max count is

Error message

Max count is {Count}

What it means

GetAt(position) throws ArgumentOutOfRangeException when the requested position is negative or >= Count. The message reports the maximum valid count so the caller can see how far off the request was. Note the message string is passed as the paramName-style first argument, so the text appears as the parameter name in the exception output.

Solutions

  1. Clamp or validate position against list.Count before calling: 0 <= position < Count.
  2. Check Count (or IsEmpty) before indexing, especially after Remove/RemoveHead operations.
  3. If the caller's input is 1-based, subtract 1 before calling GetAt.

Example fix

// before
var node = list.GetAt(list.Count); // off by one
// after
if (list.Count > 0)
    var node = list.GetAt(list.Count - 1);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool IsValidPosition(int p, int count) => p >= 0 && p < count;

Try / catch

try { var node = list.GetAt(position); }
catch (ArgumentOutOfRangeException) { /* handle missing index */ }

Prevention

When it happens

Trigger: Calling GetAt with a negative integer, or calling GetAt(Count) or higher on a list with Count elements. Also calling GetAt on an empty list (Count == 0) with any position >= 0.

Common situations: Off-by-one loops (iterating i <= Count), using 1-based positions from user input against a 0-based API, or calling GetAt after removals emptied the list.

Related errors


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

Appendix: source

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

            }

            current = current.Next;
        }

        throw new ItemNotFoundException();
    }

    /// <summary>
    ///     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)
        {

View on GitHub (pinned to 96e2905cab)