stride3d/stride · error · ArgumentException

index

Error message

index

What it means

MultiValueSortedDictionary's non-generic ICollection.CopyTo validates the destination array and index, then throws ArgumentException("index") when the index is at or past the array's end, or when the dictionary's element count does not fit between index and the array length. It uses ArgumentException with the message 'index' rather than a more descriptive message.

Solutions

  1. Allocate the array with length >= index + dictionary Count before copying
  2. Compute required size: array = new KeyValuePair<TKey,TValue>[dict.Count + index]
  3. Copy at index 0 into a correctly sized array when no offset is needed
  4. Catch ArgumentException and re-copy into a larger array if you cannot pre-size

Example fix

// before
var arr = new KeyValuePair<TKey,TValue>[dict.Count];
dict.CopyTo(arr, 5); // throws: 5 + Count > arr.Length
// after
var arr = new KeyValuePair<TKey,TValue>[dict.Count + 5];
dict.CopyTo(arr, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || index < 0 || index >= array.Length || dict.Count > array.Length - index) throw new ArgumentException("Array too small for CopyTo");
dict.CopyTo(array, index);

Try / catch

try { dict.CopyTo(array, index); } catch (ArgumentException) { array = new KeyValuePair<TKey,TValue>[dict.Count + index]; dict.CopyTo(array, index); }

Prevention

When it happens

Trigger: Calling CopyTo(array, index) where index >= array.Length, or where index < array.Length but Count elements cannot fit starting at index (i.e. Count > array.Length - index).

Common situations: Copying into a fixed-size buffer sized without accounting for the starting offset; copying multiple collections into one array with an index that leaves too little room; passing a default-sized array.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core/Collections/MultiValueSortedDictionary.cs:283

    /// </param>
    /// <exception cref="System.ArgumentNullException">array is null.</exception>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// index is equal to or greater than the length of array.  -or- index is less
    /// than 0.
    /// </exception>
    /// <exception cref="System.ArgumentException">
    /// The number of elements in the source KoderHack.MultiValueSortedDictionary&lt;TKey,TValue&gt;
    /// is greater than the available space from index to the end of the destination
    /// array.
    /// </exception>
    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
    {
        if (array == null)
            throw new ArgumentNullException(nameof(array));
        if (index < 0)
            throw new ArgumentOutOfRangeException(nameof(index));
        if (index >= array.Length || _Count > array.Length - index)
            throw new ArgumentException("index");

        var copyIndex = index;
        foreach (var pair in this)
            array[copyIndex++] = new KeyValuePair<TKey, TValue>(pair.Key, pair.Value);
    }

    /// <summary>
    /// Returns an enumerator that iterates through the KoderHack.MultiValueSortedDictionary&lt;TKey,TValue&gt;.
    /// </summary>
    /// <returns>
    /// A KoderHack.MultiValueSortedDictionary&lt;TKey,TValue&gt;.Enumerator for
    /// the KoderHack.MultiValueSortedDictionary&lt;TKey,TValue&gt;.
    /// </returns>
    public Enumerator GetEnumerator()
    {
        _IsModified = false;
        return new Enumerator(this);
    }

View on GitHub (pinned to 96fad776d2)