{"record":{"id":"25a194f79eff8c51","repo":"AvaloniaUI/Avalonia","slug":"invalid-array-type-25a194","errorCode":null,"errorMessage":"Invalid array type.","messagePattern":"Invalid array type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs","lineNumber":282,"sourceCode":"            throw new NotSupportedException();\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowAggregateException(List<Exception> exceptions)\n        {\n            throw new AggregateException(exceptions);\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowOutOfMemoryException()\n        {\n            throw new OutOfMemoryException();\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowArgumentException_Argument_InvalidArrayType()\n        {\n            throw new ArgumentException(\"Invalid array type.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted()\n        {\n            throw new InvalidOperationException(\"Enumeration has not started.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()\n        {\n            throw new InvalidOperationException(\"Enumeration has ended.\");\n        }\n\n        [DoesNotReturn]\n        internal static void ThrowInvalidOperationException_EnumCurrent(int index)\n        {\n            throw GetInvalidOperationException_EnumCurrent(index);","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/Pooled/ThrowHelper.cs#L264-L300","documentation":"Centralized ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType throws ArgumentException \"Invalid array type.\" It is reached from the non-generic ICollection.CopyTo on both PooledList<T> and PooledStack<T> inside a `catch (ArrayTypeMismatchException)` block: Array.Copy throws ArrayTypeMismatchException when the destination array's element type is not compatible with T, and this helper rewraps it as a clearer ArgumentException.","triggerScenarios":"Calling `((ICollection)list).CopyTo(wrongTypeArray, index)` where the array element type is not assignment-compatible with T — e.g. copying a PooledList<string> into an int[], or a PooledList<Derived> into a Base[] that the runtime rejects (covariance edge cases).","commonSituations":"Non-generic code passing an object[]/wrongly-typed array through ICollection.CopyTo; reflection or dynamic typing producing a mismatched array type; covariance surprises with reference arrays.","solutions":["Use a correctly typed array: `var arr = new T[list.Count]; ((ICollection)list).CopyTo(arr, 0);`.","Prefer the generic CopyTo(T[], int) overload, which is type-checked at compile time.","When using the non-generic interface, validate `array.GetType().GetElementType()` is compatible with T."],"exampleFix":"// before\nobject[] arr = new object[list.Count];\n((ICollection)list).CopyTo(arr, 0); // type mismatch\n\n// after\nT[] arr = new T[list.Count];\nlist.CopyTo(arr, 0);","handlingStrategy":"type-guard","validationCode":"var arr = new T[list.Count];\nlist.CopyTo(arr, 0); // compile-time type-safe, avoids the non-generic path","typeGuard":"static bool IsCompatibleArray<T>(Array a)\n    => a is not null && a.Rank == 1\n       && typeof(T).IsAssignableFrom(a.GetType().GetElementType());","tryCatchPattern":null,"preventionTips":["Prefer the generic CopyTo(T[], int) over ICollection.CopyTo.","When using the non-generic interface, verify the element type matches T.","Avoid object[] as a destination for value-typed collections."],"tags":["csharp","pooled","collections","array-type","copy"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}