TheAlgorithms/C-Sharp · error · ArgumentException

Position is out of size

Error message

Position is out of size

What it means

UnrolledLinkedListNode.Set throws ArgumentException when the requested position is negative or beyond the node's internal array capacity (pos > array.Length - 1). The node's backing array has fixed capacity, so only indices 0..Length-1 are valid write positions.

Solutions

  1. Validate 0 <= pos < node's array capacity before calling Set
  2. Convert a global list position to the correct node-local offset before writing
  3. Catch ArgumentException at a higher level if the position source is untrusted

Example fix

// before
node.Set(index, value); // index is a list-level position
// after
if (index < 0 || index >= node.ArrayLength)
{
    throw new ArgumentOutOfRangeException(nameof(index));
}
node.Set(index, value);
Defensive patterns

Strategy: validation

Validate before calling

if (pos < 0 || pos >= node.ArrayLength)
    throw new ArgumentOutOfRangeException(nameof(pos), "Position outside node capacity.");
node.Set(pos, val);

Try / catch

try { node.Set(pos, val); }
catch (ArgumentException ex) { /* map to user-facing 'index out of range' message */ }

Prevention

When it happens

Trigger: Calling Set(pos, val) with pos < 0 or pos >= the node's array length — e.g. Set(5, x) on a node whose array length is 5, or Set(-1, x).

Common situations: Off-by-one when writing at the last slot (using array.Length instead of Length-1); passing a global list index instead of the node-local offset; inserting into an unrolled linked list with an incorrectly computed node-local position.

Related errors


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

Appendix: source

Thrown at DataStructures/UnrolledList/UnrolledLinkedListNode.cs:24

public class UnrolledLinkedListNode(int nodeSize)
{
    private readonly int[] array = new int[nodeSize];

    public UnrolledLinkedListNode Next { get; set; } = null!;

    public int Count { get; set; }

    /// <summary>
    /// Set new item in array buffer.
    /// </summary>
    /// <param name="pos">Index in array.</param>
    /// <param name="val">The entered value.</param>
    /// <exception cref="ArgumentException">Index is out of scope.</exception>
    public void Set(int pos, int val)
    {
        if (pos < 0 || pos > array.Length - 1)
        {
            throw new ArgumentException("Position is out of size", nameof(pos));
        }

        array[pos] = val;
        Count++;
    }

    /// <summary>
    /// Get item from array buffer.
    /// </summary>
    /// <param name="pos">Index in array.</param>
    /// <exception cref="ArgumentException">Index is out of scope.</exception>
    public int Get(int pos)
    {
        if (pos < 0 || pos > array.Length - 1)
        {
            throw new ArgumentException("Position is out of size", nameof(pos));
        }

View on GitHub (pinned to 96e2905cab)