{"record":{"id":"bc0d346d9e24b506","repo":"dotnet/wpf","slug":"sr-collection-copyto-indexgreaterthanorequaltoarraylength-bc0d34","errorCode":null,"errorMessage":"SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","messagePattern":"SR\\.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":121,"sourceCode":"        /// </summary>\n        [CLSCompliant(false)]\n        public bool Contains(KeyValuePair<int, CharacterMetrics> item)\n        {\n            return item.Value != null && item.Value.Equals(GetValue(item.Key));\n        }\n\n        /// <summary>\n        /// Copies the contents of the collection to the specified array.\n        /// </summary>\n        [CLSCompliant(false)]\n        public void CopyTo(KeyValuePair<int, CharacterMetrics>[] array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            CharacterMetrics[][] pageTable = _pageTable;\n            if (pageTable != null)\n            {\n                int k = index;\n\n                for (int i = 0; i < pageTable.Length; ++i)\n                {\n                    CharacterMetrics[] page = pageTable[i];\n                    if (page != null)\n                    {\n                        for (int j = 0; j < page.Length; ++j)\n                        {\n                            CharacterMetrics metrics = page[j];\n                            if (metrics != null)\n                            {\n                                if (k >= array.Length)\n                                    throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L103-L139","documentation":"CharacterMetricsDictionary.CopyTo(KeyValuePair<int,CharacterMetrics>[] array, int index) validates the destination index. If index is greater than or equal to array.Length, no element can ever be written and an ArgumentException is thrown. (A null array or negative index fails earlier with different exceptions.)","triggerScenarios":"Calling dictionary.CopyTo(array, index) where index >= array.Length, e.g. CopyTo(new KeyValuePair<int,CharacterMetrics>[10], 10) or CopyTo(someArray, someArray.Length).","commonSituations":"Computing a start index from a cursor that has advanced past the buffer; off-by-one when copying into the tail of a larger array; passing an empty array with index 0.","solutions":["Pass an index strictly less than array.Length (and >= 0).","Ensure the array is large enough: index + Count <= array.Length.","Guard the call with `if (index < array.Length)` before invoking CopyTo."],"exampleFix":"// before\ndict.CopyTo(pairs, pairs.Length);\n// after\nif (pairs.Length > 0) dict.CopyTo(pairs, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (index < 0 || index >= array.Length)\n    throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":"try { dict.CopyTo(array, index); }\ncatch (ArgumentException ex) { /* fix index/array sizing, retry */ }","preventionTips":["Assert index < array.Length before CopyTo.","Prefer copying into a right-sized array at index 0.","Beware empty arrays with any index."],"tags":["wpf","copyto","argument-exception","bounds"],"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"}