{"record":{"id":"f74d74d8bf403b1f","repo":"dotnet/wpf","slug":"sr-format-sr-textelementcollection-f74d74","errorCode":null,"errorMessage":"SR.Format(SR.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array.Length)","messagePattern":"SR\\.Format\\(SR\\.TextElementCollection_CannotCopyToArrayNotSufficientMemory, count, arrayIndex, array\\.Length\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":614,"sourceCode":"\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            }\n        }\n\n        int ICollection.Count\n        {\n            get \n            {\n                return this.Count;\n            }\n        }\n\n        bool ICollection.IsSynchronized\n        {","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L596-L632","documentation":"CopyTo throws ArgumentException (TextElementCollection_CannotCopyToArrayNotSufficientMemory) when the target array is too small: array.Length < arrayIndex + count, where count is the number of elements in the collection. The copy loop would write past the end of the array, so it is rejected before any element is copied.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where array.Length - arrayIndex is less than the collection's Count.","commonSituations":"Reusing a cached array after the document grew (more paragraphs added); reserving only part of a larger buffer but forgetting the offset consumes space; copying between documents of different sizes.","solutions":["Size the array to list.Count plus the offset: new TextElementType[list.Count + arrayIndex].","Call list.Count right before CopyTo rather than using a stale count.","Use list.ToArray<T>() which allocates the exact size.","Catch ArgumentException and retry with an array of Count + arrayIndex elements."],"exampleFix":"// before\nvar arr = new Paragraph[oldCount];\nlist.CopyTo(arr, 0);\n// after\nvar arr = new Paragraph[list.Count];\nlist.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array.Length - arrayIndex < list.Count) { array = new TextElementType[list.Count + arrayIndex]; }","typeGuard":null,"tryCatchPattern":"try { list.CopyTo(arr, offset); } catch (ArgumentException) { arr = new TextElementType[list.Count + offset]; list.CopyTo(arr, offset); }","preventionTips":["Size buffers as Count + offset right before copying","Re-read Count after any document mutation","Prefer list.ToArray<T>()"],"tags":["wpf","argument","copyto","capacity"],"backgroundTag":"insufficient-buffer-size","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"}