{"record":{"id":"2cf6a12c17d91467","repo":"dotnet/wpf","slug":"sr-collection-copyto-numberofelementsexceedsarraylength-2cf6a1","errorCode":null,"errorMessage":"SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength (formatted with \"arrayIndex\", \"array\")","messagePattern":"SR\\.Collection_CopyTo_NumberOfElementsExceedsArrayLength \\(formatted with \"arrayIndex\", \"array\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs","lineNumber":139,"sourceCode":"            if (mapping.Uri == null)\n                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));\n\n            return (this.LookupNamespace(mapping.Prefix) == mapping.Uri.OriginalString);\n        }\n\n        /// <summary>\n        /// Copy XmlNamespaceMappings to array\n        /// </summary>\n        public void CopyTo(XmlNamespaceMapping[] array, int arrayIndex)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            int i = arrayIndex;\n            int maxLength = array.Length;\n            foreach (XmlNamespaceMapping mapping in this)\n            {\n                if (i >= maxLength)\n                    throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, nameof(arrayIndex), nameof(array)));\n                array[i] = mapping;\n                ++ i;\n            }\n        }\n\n        /// <summary>\n        /// Remove XmlNamespaceMapping\n        /// </summary>\n        /// <returns>\n        /// true if the mapping was removed\n        /// </returns>\n        /// <exception cref=\"ArgumentNullException\">mapping is null</exception>\n        public bool Remove(XmlNamespaceMapping mapping)\n        {\n            ArgumentNullException.ThrowIfNull(mapping);\n\n            if (mapping.Uri == null)\n                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs#L121-L157","documentation":"XmlNamespaceMappingCollection.CopyTo throws ArgumentException when the destination array is too small to hold all mappings starting at arrayIndex. The loop writes mappings into array[i] and throws once the index reaches array.Length. This is the standard ICollection.CopyTo bounds check.","triggerScenarios":"Calling CopyTo(array, arrayIndex) where array.Length - arrayIndex is less than the number of mappings in the collection.","commonSituations":"Reusing a cached array after the collection grew; computing array size without accounting for arrayIndex; passing a default-sized (0-length) array.","solutions":["Allocate the array with size collection.Count + arrayIndex before copying","Re-check collection.Count immediately before CopyTo if the collection can change","Use ToArray()/ToList() instead of manual CopyTo"],"exampleFix":"// before\nvar arr = new XmlNamespaceMapping[collection.Count];\ncollection.CopyTo(arr, 5); // throws: only Count-5 slots available\n// after\nvar arr = new XmlNamespaceMapping[collection.Count + 5];\ncollection.CopyTo(arr, 5);","handlingStrategy":"validation","validationCode":"if (array.Length - arrayIndex < collection.Count) array = new XmlNamespaceMapping[collection.Count + arrayIndex];","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, arrayIndex); } catch (ArgumentException) { array = new XmlNamespaceMapping[collection.Count + arrayIndex]; collection.CopyTo(array, arrayIndex); }","preventionTips":["Size arrays from collection.Count + arrayIndex at call time","Re-snapshot Count if the collection may change between sizing and copying","Use LINQ ToArray/ToList instead of raw CopyTo"],"tags":["wpf","xml-databinding","array-bounds"],"backgroundTag":"index-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"}