stride3d/stride · error · ArgumentException

Destination array is of incorrect type.

Error message

Destination array is of incorrect type.

What it means

Inside Deque<T>'s non-generic ICollection.CopyTo, each element is written with Array.SetValue; if an element cannot be cast to the destination array's element type, the caught InvalidCastException is rethrown as ArgumentException('Destination array is of incorrect type.'). It surfaces when the deque is used through the non-generic interface with a destination array of an incompatible element type.

Solutions

  1. Pass an array whose element type matches T (or is assignable from every deque element).
  2. Prefer the generic ICollection<T>.CopyTo, which enforces T[] at compile time.
  3. Pre-check array.GetType().GetElementType() compatibility with typeof(T) before copying.

Example fix

// before
Array dest = new string[deque.Count];
((ICollection)deque).CopyTo(dest, 0); // InvalidCastException -> ArgumentException

// after
Array dest = new int[deque.Count]; // element type matches Deque<int>'s T
((ICollection)deque).CopyTo(dest, 0);
Defensive patterns

Strategy: type-guard

Validate before calling

var elementType = array.GetType().GetElementType();
if (elementType == null || !elementType.IsAssignableFrom(typeof(T)))
    throw new ArgumentException($"Destination array element type {elementType} cannot hold {typeof(T)} items");

Type guard

static bool IsCompatibleDestination<T>(Array array) =>
    array.GetType().GetElementType() is { } et && et.IsAssignableFrom(typeof(T));

Try / catch

try
{
    ((ICollection)deque).CopyTo(array, index);
}
catch (ArgumentException ex) when (ex.Message.Contains("incorrect type"))
{
    var typed = new T[deque.Count];
    ((ICollection<T>)deque).CopyTo(typed, 0);
}

Prevention

When it happens

Trigger: ((ICollection)dequeOfInt).CopyTo(new string[n], 0); copying a Deque<object> holding heterogeneous items into a narrower typed array; refactors that changed the deque's element type but not the destination buffer.

Common situations: Non-generic collection interop with mismatched buffers; reflection/serialization code writing into typed arrays; covariance assumptions that don't hold for value types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at sources/core/Stride.Core/Collections/Dequeue.cs:368

            this[index] = (T)value;
        }
    }

    void System.Collections.ICollection.CopyTo(Array array, int index)
    {
        if (array == null)
            throw new ArgumentNullException(nameof(array), "Destination array cannot be null.");
        CheckRangeArguments(array.Length, index, Count);

        for (int i = 0; i != Count; ++i)
        {
            try
            {
                array.SetValue(this[i], index + i);
            }
            catch (InvalidCastException ex)
            {
                throw new ArgumentException("Destination array is of incorrect type.", ex);
            }
        }
    }

    bool System.Collections.ICollection.IsSynchronized => false;

    object System.Collections.ICollection.SyncRoot => this;

    #endregion

    #region GenericListHelpers

    /// <summary>
    /// Checks the <paramref name="index"/> argument to see if it refers to a valid insertion point in a source of a given length.
    /// </summary>
    /// <param name="sourceLength">The length of the source. This parameter is not checked for validity.</param>
    /// <param name="index">The index into the source.</param>
    /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index to an insertion point for the source.</exception>

View on GitHub (pinned to 96fad776d2)