stride3d/stride · error · ArgumentException

array is multi-dimensional

Error message

array is multi-dimensional

What it means

This CopyTo overload validates that the destination array is one-dimensional (array.Rank <= 1) before writing, because element-by-element indexing cannot address multi-dimensional arrays. A multi-dimensional array (e.g. T[,] ) triggers this ArgumentException.

Solutions

  1. Pass a one-dimensional array: var flat = new KeyValuePair<TKey,TValue>[sl.Count].
  2. If you need 2D layout, copy into a flat 1D array first and then map indices yourself.
  3. Add array.Rank == 1 check in generic helper code before calling CopyTo.

Example fix

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

Strategy: type-guard

Validate before calling

if (array == null || array.Rank != 1)
    throw new ArgumentException("Destination must be a 1D array.", nameof(array));

Type guard

static bool Is1DKvpArray(object a) =>
    a is KeyValuePair<TKey,TValue>[] and { Rank: 1 };

Try / catch

try { sortedList.CopyTo(array, arrayIndex); }
catch (ArgumentException ex) when (ex.Message.Contains("multi-dimensional"))
{ var flat = new KeyValuePair<TKey,TValue>[sortedList.Count]; sortedList.CopyTo(flat, 0); }

Prevention

When it happens

Trigger: Calling the non-explicit-interface CopyTo(new T[2,2], 0) or any array with Rank > 1.

Common situations: Passing a rectangular 2D grid buffer because its total Length looks sufficient, or generic code that receives Array and does not check Rank.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

            return;
        var i = IndexOfKey(key1);
        if (i >= 0) RemoveAt(i);
    }

    // ICollection

    void ICollection.CopyTo(Array array, int arrayIndex)
    {
        if (Count == 0)
            return;

        ArgumentNullException.ThrowIfNull(array);

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

        if (array.Rank > 1)
            throw new ArgumentException("array is multi-dimensional");
        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");

        using var it = GetEnumerator();
        var i = arrayIndex;

        while (it.MoveNext())
        {
            array.SetValue(it.Current, i++);
        }
    }

    //
    // SortedList<TKey, TValue>
    //

View on GitHub (pinned to 96fad776d2)