{"record":{"id":"46ff323e2ecb6067","repo":"stride3d/stride","slug":"destination-array-is-of-incorrect-type","errorCode":null,"errorMessage":"Destination array is of incorrect type.","messagePattern":"Destination array is of incorrect type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/Dequeue.cs","lineNumber":368,"sourceCode":"            this[index] = (T)value;\n        }\n    }\n\n    void System.Collections.ICollection.CopyTo(Array array, int index)\n    {\n        if (array == null)\n            throw new ArgumentNullException(nameof(array), \"Destination array cannot be null.\");\n        CheckRangeArguments(array.Length, index, Count);\n\n        for (int i = 0; i != Count; ++i)\n        {\n            try\n            {\n                array.SetValue(this[i], index + i);\n            }\n            catch (InvalidCastException ex)\n            {\n                throw new ArgumentException(\"Destination array is of incorrect type.\", ex);\n            }\n        }\n    }\n\n    bool System.Collections.ICollection.IsSynchronized => false;\n\n    object System.Collections.ICollection.SyncRoot => this;\n\n    #endregion\n\n    #region GenericListHelpers\n\n    /// <summary>\n    /// Checks the <paramref name=\"index\"/> argument to see if it refers to a valid insertion point in a source of a given length.\n    /// </summary>\n    /// <param name=\"sourceLength\">The length of the source. This parameter is not checked for validity.</param>\n    /// <param name=\"index\">The index into the source.</param>\n    /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"index\"/> is not a valid index to an insertion point for the source.</exception>","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/Dequeue.cs#L350-L386","documentation":"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.","triggerScenarios":"((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.","commonSituations":"Non-generic collection interop with mismatched buffers; reflection/serialization code writing into typed arrays; covariance assumptions that don't hold for value types.","solutions":["Pass an array whose element type matches T (or is assignable from every deque element).","Prefer the generic ICollection<T>.CopyTo, which enforces T[] at compile time.","Pre-check array.GetType().GetElementType() compatibility with typeof(T) before copying."],"exampleFix":"// before\nArray dest = new string[deque.Count];\n((ICollection)deque).CopyTo(dest, 0); // InvalidCastException -> ArgumentException\n\n// after\nArray dest = new int[deque.Count]; // element type matches Deque<int>'s T\n((ICollection)deque).CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"var elementType = array.GetType().GetElementType();\nif (elementType == null || !elementType.IsAssignableFrom(typeof(T)))\n    throw new ArgumentException($\"Destination array element type {elementType} cannot hold {typeof(T)} items\");","typeGuard":"static bool IsCompatibleDestination<T>(Array array) =>\n    array.GetType().GetElementType() is { } et && et.IsAssignableFrom(typeof(T));","tryCatchPattern":"try\n{\n    ((ICollection)deque).CopyTo(array, index);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"incorrect type\"))\n{\n    var typed = new T[deque.Count];\n    ((ICollection<T>)deque).CopyTo(typed, 0);\n}","preventionTips":["Use the generic ICollection<T>.CopyTo so element-type mismatches become compile errors.","Match destination array element type exactly to the deque's T.","Update destination buffers whenever the deque's element type changes in a refactor.","Do not rely on array covariance for value types."],"tags":["csharp","stride-core","collections","type-safety"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}