{"record":{"id":"8c8fac33d394b05e","repo":"dotnet/wpf","slug":"array","errorCode":null,"errorMessage":"array","messagePattern":"array","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":602,"sourceCode":"                    this.TextContainer.EndChange();\n                }\n            }\n        }\n\n        #endregion IList Members\n\n        #region ICollection Members\n\n        void ICollection.CopyTo(Array array, int arrayIndex)\n        {\n            int count = this.Count;\n\n            ArgumentNullException.ThrowIfNull(array);\n\n            Type elementType = array.GetType().GetElementType();\n            if (elementType == null || !elementType.IsAssignableFrom(typeof(TextElementType)))\n            {\n                throw new ArgumentException(\"array\");\n            }\n\n            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);\n\n            if (arrayIndex > array.Length)\n            {\n                throw new ArgumentException(\"arrayIndex\");\n            }\n\n            if (array.Length < arrayIndex + count)\n            {\n                throw new ArgumentException(SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array.Length));\n            }\n\n            for (TextElementType element = (TextElementType)this.FirstChild; element != null; element = (TextElementType)element.NextElement)\n            {\n                array.SetValue(element, arrayIndex++);\n            }","sourceCodeStart":584,"sourceCodeEnd":620,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L584-L620","documentation":"CopyTo(array, arrayIndex) throws ArgumentException(\"array\") when the array's element type cannot hold TextElementType instances — either the array is a non-array object (GetElementType returns null) or its element type is not assignable from TextElementType. Copying text elements into, say, a string[] or int[] is rejected up front.","triggerScenarios":"Calling CopyTo with an array whose element type is narrower or unrelated to the collection's element type (e.g. string[], object[] is fine only if assignable — unrelated types throw).","commonSituations":"Copying into a generic object[] where the element type check fails on strict array covariance rules; passing a 2D array or jagged array whose GetElementType is an array type; copy/paste utility code that assumed a compatible buffer.","solutions":["Allocate the target array with the collection's element type: new TextElementType[list.Count].","Use OfType<T>().ToArray() or CopyTo into a typed array instead of manually constructing one.","Verify array.GetType().GetElementType().IsAssignableFrom(typeof(TextElementType)) before calling CopyTo.","Catch ArgumentException and retry with a correctly typed array."],"exampleFix":"// before\nvar arr = new string[list.Count];\n((ICollection)list).CopyTo(arr, 0);\n// after\nvar arr = new Paragraph[list.Count];\nlist.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array.GetType().GetElementType()?.IsAssignableFrom(typeof(TextElement)) != true) throw new ArgumentException(\"Incompatible array element type\", nameof(array));","typeGuard":"static bool IsCompatibleArray(Array a) => a.GetType().GetElementType()?.IsInstanceOfType(Activator.CreateInstance(a.GetType().GetElementType()!)) ?? false;","tryCatchPattern":"try { list.CopyTo(arr, 0); } catch (ArgumentException) { arr = new TextElementType[list.Count]; list.CopyTo(arr, 0); }","preventionTips":["Allocate arrays with the collection's element type","Use ToArray<T>() instead of manual buffers","Only pass single-dimensional, correctly typed arrays to CopyTo"],"tags":["wpf","argument","copyto"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}