{"record":{"id":"aff17835e5dfcb0c","repo":"dotnet/wpf","slug":"arrayindex","errorCode":null,"errorMessage":"arrayIndex","messagePattern":"arrayIndex","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":609,"sourceCode":"        #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            }\n        }\n\n        int ICollection.Count\n        {\n            get \n            {\n                return this.Count;","sourceCodeStart":591,"sourceCodeEnd":627,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L591-L627","documentation":"CopyTo throws ArgumentException(\"arrayIndex\") when arrayIndex is negative or greater than the target array's length. ArgumentOutOfRangeException.ThrowIfNegative handles the negative case; the explicit check handles arrayIndex > array.Length. Either leaves no valid contiguous region to write into.","triggerScenarios":"Calling CopyTo(array, arrayIndex) with arrayIndex < 0 or arrayIndex > array.Length (arrayIndex == array.Length is allowed by this check but then fails the capacity check unless count == 0).","commonSituations":"Off-by-one arithmetic producing index == Length + 1; passing an uninitialized sentinel value (-1) as the offset; resuming a copy into a resized array with a stale offset.","solutions":["Validate 0 <= arrayIndex <= array.Length before calling CopyTo.","Use arrayIndex = 0 (or list.CopyTo(array)) for full-collection copies.","Clamp: arrayIndex = Math.Clamp(arrayIndex, 0, array.Length)."],"exampleFix":"// before\nlist.CopyTo(arr, -1);\n// after\nint start = Math.Max(0, Math.Min(offset, arr.Length));\nlist.CopyTo(arr, start);","handlingStrategy":"validation","validationCode":"if (arrayIndex < 0 || arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp offsets to [0, array.Length]","Default to arrayIndex 0 for full copies","Never pass sentinel values like -1 as offsets"],"tags":["wpf","argument-out-of-range","copyto"],"backgroundTag":"argument-out-of-range","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"}