dotnet/machinelearning · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values. (Pa

Error message

Specified argument was out of the range of valid values. (Parameter 'index')

What it means

The internal indexer setter validates that index is within the currently written Length of the buffer and throws ArgumentOutOfRangeException otherwise. You may only overwrite values already appended; setting beyond Length is undefined for the buffer layout.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameBuffer.cs:97

            if (newLength > Capacity)
            {
                //Double buffer size, but not higher than MaxByteCapacity
                var doubledSize = (int)Math.Min((long)ReadOnlyBuffer.Length * 2, ArrayUtility.ArrayMaxSize);
                var newCapacity = Math.Max(newLength * Size, doubledSize);

                var memory = new Memory<byte>(new byte[newCapacity]);
                _memory.CopyTo(memory);
                _memory = memory;
            }
        }

        internal override T this[int index]
        {
            set
            {
                if (index >= Length)
                    throw new ArgumentOutOfRangeException(nameof(index));

                RawSpan[index] = value;
            }
        }

        internal static DataFrameBuffer<T> GetMutableBuffer(ReadOnlyDataFrameBuffer<T> buffer)
        {
            DataFrameBuffer<T> mutableBuffer = buffer as DataFrameBuffer<T>;
            if (mutableBuffer == null)
            {
                mutableBuffer = new DataFrameBuffer<T>(buffer.ReadOnlyBuffer, buffer.Length);
            }
            return mutableBuffer;
        }
    }
}

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Append values instead of writing by index until Length covers the target
  2. Ensure index < Length before assignment (or extend via Append/EnsureCapacity first)
  3. Fix loop bounds to iterate up to Length, not Capacity

Example fix

// before
for (int i = 0; i < buffer.Capacity; i++) buffer[i] = data[i];
// after
for (int i = 0; i < buffer.Length; i++) buffer[i] = data[i];
Defensive patterns

Strategy: type-guard

Validate before calling

bool CanWriteAt(DataFrameBuffer<T> b, int i) => i >= 0 && i < b.Length;

Type guard

bool IsValidIndex<T>(DataFrameBuffer<T> buffer, int index) => index >= 0 && index < buffer.Length;

Try / catch

try { buffer[index] = value; }
catch (ArgumentOutOfRangeException) { /* ensure Length covers index via Append/EnsureCapacity first */ }

Prevention

When it happens

Trigger: Setting DataFrameBuffer<T>[index] with index >= Length (src/Microsoft.Data.Analysis/DataFrameBuffer.cs:97), e.g. writing at Capacity indices or a stale index after the buffer was reset/shrunk.

Common situations: Pre-allocating a large buffer and trying to fill by index before appending; off-by-one loops using Capacity instead of Length; concurrent writers using divergent index counters.

Related errors


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