{"record":{"id":"b6ee01e584dc06e2","repo":"AvaloniaUI/Avalonia","slug":"the-target-array-is-too-small","errorCode":null,"errorMessage":"The target array is too small.","messagePattern":"The target array is too small\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/AvaloniaList.cs","lineNumber":681,"sourceCode":"\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(\"Multi-dimensional arrays are not supported.\");\n            }\n\n            if (array.GetLowerBound(0) != 0)\n            {\n                throw new ArgumentException(\"Non-zero lower bounds are not supported.\");\n            }\n\n            if (index < 0)\n            {\n                throw new ArgumentException(\"Invalid index.\");\n            }\n\n            if (array.Length - index < Count)\n            {\n                throw new ArgumentException(\"The target array is too small.\");\n            }\n\n            if (array is T[] tArray)\n            {\n                _inner.CopyTo(tArray, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can't find all possible problems by doing the check though.\n                // For example, if the element type of the Array is derived from T,\n                // we can't figure out if we can successfully copy the element beforehand.\n                //\n                Type targetType = array.GetType().GetElementType()!;\n                Type sourceType = typeof(T);\n                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))\n                {","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/AvaloniaList.cs#L663-L699","documentation":"ArgumentException thrown by AvaloniaList.ICollection.CopyTo when the destination array does not have enough remaining space from the starting index to hold all Count elements of the list.","triggerScenarios":"Calling CopyTo with an index/array combination where array.Length - index < list.Count; e.g. a 5-element array starting at index 3 for a 4-item list.","commonSituations":"Under-allocating the destination array (using an old count), reusing a shared buffer that is too small, or an index that is not 0 leaving insufficient tail room.","solutions":["Allocate the destination as new T[list.Count] and use index 0.","Ensure array.Length - index >= list.Count before calling CopyTo.","Recompute the array size from the current Count immediately before copying."],"exampleFix":"// before\nvar arr = new T[5];\n((ICollection)list).CopyTo(arr, 3); // list.Count == 4\n\n// after\nvar arr = new T[list.Count];\n((ICollection)list).CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array.Length - index < list.Count) throw new ArgumentException(\"Destination too small.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Size the destination as new T[list.Count] and use index 0.","Recompute size from current Count right before copying.","Verify array.Length - index >= Count at the call site."],"tags":["avalonia","collections","list","copyto","capacity"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}