stride3d/stride · error · ArgumentNullException

Not enough space in array from arrayIndex to end of array

Error message

Not enough space in array from arrayIndex to end of array

What it means

CopyTo throws this when the number of elements in the SortedList exceeds the space available in the destination array starting at arrayIndex (Count > array.Length - arrayIndex). The guard prevents partially overwriting the caller's array. The exception type used is ArgumentNullException (a mislabel), but the condition is an insufficient-capacity problem.

Solutions

  1. Reallocate the destination array with length >= arrayIndex + sortedList.Count.
  2. Call ToArray() on the SortedList instead of manual CopyTo when you just need a snapshot.
  3. Recompute Count immediately before CopyTo if the collection may be modified concurrently.

Example fix

// before
var buf = new KeyValuePair<string,int>[3];
sl.CopyTo(buf, 0); // sl.Count == 5
// after
var buf = new KeyValuePair<string,int>[sl.Count];
sl.CopyTo(buf, 0);
Defensive patterns

Strategy: validation

Validate before calling

int required = sortedList.Count + arrayIndex;
if (array == null || array.Length < required)
    array = new KeyValuePair<TKey,TValue>[required];
sortedList.CopyTo(array, arrayIndex);

Type guard

static bool HasRoom(int arrayLength, int arrayIndex, int count) =>
    arrayLength - arrayIndex >= count;

Try / catch

try { sortedList.CopyTo(array, arrayIndex); }
catch (ArgumentNullException) when (array.Length - arrayIndex < sortedList.Count)
{ array = new KeyValuePair<TKey,TValue>[sortedList.Count + arrayIndex]; sortedList.CopyTo(array, arrayIndex); }

Prevention

When it happens

Trigger: CopyTo(array, arrayIndex) where array.Length - arrayIndex < sortedList.Count, e.g. a 5-element list copied into a 3-element array.

Common situations: Destination array reused across iterations as the list grew, off-by-one in offset computation, or snapshotting a collection whose Count changed since the array was allocated.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/348288ed80459748. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/SortedList.cs:278

        this.table = new KeyValuePair<TKey, TValue>[defaultCapacity];
        inUse = 0;
        modificationCount++;
    }

    void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
        if (Count == 0)
            return;

        ArgumentNullException.ThrowIfNull(array);

        if (arrayIndex < 0)
            throw new ArgumentOutOfRangeException();

        if (arrayIndex >= array.Length)
            throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
        if (Count > (array.Length - arrayIndex))
            throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");

        var i = arrayIndex;
        foreach (var pair in this)
            array[i++] = pair;
    }

    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)
    {
        Add(keyValuePair.Key, keyValuePair.Value);
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
    {
        var i = Find(keyValuePair.Key);

        if (i >= 0)
            return Comparer<KeyValuePair<TKey, TValue>>.Default.Compare(table[i], keyValuePair) == 0;
        return false;

View on GitHub (pinned to 96fad776d2)