egametang/ET · error · ArgumentOutOfRangeException

ArgumentOutOfRange_Index

Error message

ArgumentOutOfRange_Index

What it means

Standard .NET BCL argument error, ported into this SortedSet<T>.CopyTo(T[] array, int index) implementation: the supplied index is negative or greater than array.Length. Mirrors the runtime SortedSet contract for the destination-array start offset.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Collection/SortedSet.cs:703

        }

        void ICollection.CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
            }

            if (array.GetLowerBound(0) != 0)
            {
                throw new ArgumentException(SR.Arg_NonZeroLowerBound, nameof(array));
            }

            if (index < 0 || index > array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
            }

            if (array.Length - index < Count)
            {
                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);
            }

            T[] tarray = array as T[];
            if (tarray != null)
            {
                CopyTo(tarray, index);
            }
            else
            {
                object[] objects = array as object[];
                if (objects == null)
                {
                    throw new ArgumentException(SR.Argument_IncompatibleArrayType, nameof(array));

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Ensure 0 <= index <= array.Length, and that index leaves room for Count elements.
  2. Prefer the single-arg CopyTo(array) overload when writing from the start.
  3. Validate the offset against the destination array's actual Length, not a source collection's count.

Example fix

// before
set.CopyTo(dest, dest.Length); // index > allowed when dest non-empty
// after
set.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

static void CopyToSafe<T>(SortedSet<T> set, T[] array, int index)
{
    if (index < 0 || index > array.Length) throw new ArgumentOutOfRangeException(nameof(index));
    if (array.Length - index < set.Count) throw new ArgumentException("destination too small");
    set.CopyTo(array, index);
}

Prevention

When it happens

Trigger: Calling CopyTo with index < 0 (e.g. -1) or index > array.Length (e.g. passing array.Length + 1, or index == array.Length when array is non-empty and a non-zero count must be written).

Common situations: Off-by-one when computing an offset; passing a base offset from another array's Length into a smaller destination; reusing an index variable from a different loop.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/5beb24fd33ecd3ca. Report an issue: GitHub.