dotnet/machinelearning · error · ArgumentOutOfRangeException

nameof(startIndex)

Error message

nameof(startIndex)

What it means

The PrimitiveDataFrameColumn<T> indexer this[startIndex, length] throws ArgumentOutOfRangeException(nameof(startIndex)) when startIndex is greater than or equal to the column's Length. Only the start index is validated here (length is delegated to _columnContainer), so an out-of-range start is the failure mode. The message names startIndex but does not include the actual value or bound.

Source

Thrown at src/Microsoft.Data.Analysis/PrimitiveDataFrameColumn.cs:201

            else if (type == typeof(uint))
                return new UInt32Array(arrowValueBuffer, arrowNullBuffer, numberOfRows, nullCount, offset);
            else if (type == typeof(ulong))
                return new UInt64Array(arrowValueBuffer, arrowNullBuffer, numberOfRows, nullCount, offset);
            else if (type == typeof(ushort))
                return new UInt16Array(arrowValueBuffer, arrowNullBuffer, numberOfRows, nullCount, offset);
            else if (type == typeof(byte))
                return new UInt8Array(arrowValueBuffer, arrowNullBuffer, numberOfRows, nullCount, offset);
            else
                throw new NotImplementedException(type.ToString());
        }

        public new IReadOnlyList<T?> this[long startIndex, int length]
        {
            get
            {
                if (startIndex >= Length)
                {
                    throw new ArgumentOutOfRangeException(nameof(startIndex));
                }
                return _columnContainer[startIndex, length];
            }
        }

        protected override IReadOnlyList<object> GetValues(long startIndex, int length)
        {
            if (startIndex >= Length)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex));
            }

            var ret = new List<object>(length);
            long endIndex = Math.Min(Length, startIndex + length);
            for (long i = startIndex; i < endIndex; i++)
            {
                ret.Add(this[i]);
            }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Clamp startIndex to column.Length - 1 (or return an empty slice) before indexing
  2. Check startIndex < column.Length explicitly before calling the indexer
  3. Recompute offsets from the same column instance whose Length is being indexed
  4. Use Length-based loops (for (long i = 0; i < column.Length; ...)) instead of hard-coded indices

Example fix

// before
var slice = column[startIndex, length]; // ArgumentOutOfRangeException if startIndex >= Length
// after
if (startIndex >= column.Length) throw new ArgumentOutOfRangeException(nameof(startIndex), startIndex, "startIndex must be < Length");
var slice = column[startIndex, (int)Math.Min(length, column.Length - startIndex)];
Defensive patterns

Strategy: validation

Validate before calling

if (startIndex < 0 || startIndex >= column.Length)
    throw new ArgumentOutOfRangeException(nameof(startIndex), $"startIndex {startIndex} must be in [0, {column.Length})");

Try / catch

try { var slice = column[startIndex, length]; }
catch (ArgumentOutOfRangeException) when (startIndex >= column.Length) { startIndex = Math.Max(0, column.Length - 1); var slice = column[startIndex, length]; }

Prevention

When it happens

Trigger: Accessing column[startIndex, length] with startIndex >= column.Length, e.g. computing offsets from a different-length column, using a 1-based index, or slicing past the end of the data.

Common situations: Pagination loops whose upper bound is computed incorrectly; copying slices between columns of different lengths; off-by-one errors after Length changes when rows are added or removed.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/5f46351c930cba91. Report an issue: GitHub.