dotnet/wpf · error · ArgumentException

SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength…

Error message

SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength (formatted with "arrayIndex", "array")

What it means

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.

Solutions

  1. Allocate the array with size collection.Count + arrayIndex before copying
  2. Re-check collection.Count immediately before CopyTo if the collection can change
  3. Use ToArray()/ToList() instead of manual CopyTo

Example fix

// before
var arr = new XmlNamespaceMapping[collection.Count];
collection.CopyTo(arr, 5); // throws: only Count-5 slots available
// after
var arr = new XmlNamespaceMapping[collection.Count + 5];
collection.CopyTo(arr, 5);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Length - arrayIndex < collection.Count) array = new XmlNamespaceMapping[collection.Count + arrayIndex];

Try / catch

try { collection.CopyTo(array, arrayIndex); } catch (ArgumentException) { array = new XmlNamespaceMapping[collection.Count + arrayIndex]; collection.CopyTo(array, arrayIndex); }

Prevention

When it happens

Trigger: Calling CopyTo(array, arrayIndex) where array.Length - arrayIndex is less than the number of mappings in the collection.

Common situations: Reusing a cached array after the collection grew; computing array size without accounting for arrayIndex; passing a default-sized (0-length) array.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2cf6a12c17d91467. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMappingCollection.cs:139

            if (mapping.Uri == null)
                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));

            return (this.LookupNamespace(mapping.Prefix) == mapping.Uri.OriginalString);
        }

        /// <summary>
        /// Copy XmlNamespaceMappings to array
        /// </summary>
        public void CopyTo(XmlNamespaceMapping[] array, int arrayIndex)
        {
            ArgumentNullException.ThrowIfNull(array);

            int i = arrayIndex;
            int maxLength = array.Length;
            foreach (XmlNamespaceMapping mapping in this)
            {
                if (i >= maxLength)
                    throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, nameof(arrayIndex), nameof(array)));
                array[i] = mapping;
                ++ i;
            }
        }

        /// <summary>
        /// Remove XmlNamespaceMapping
        /// </summary>
        /// <returns>
        /// true if the mapping was removed
        /// </returns>
        /// <exception cref="ArgumentNullException">mapping is null</exception>
        public bool Remove(XmlNamespaceMapping mapping)
        {
            ArgumentNullException.ThrowIfNull(mapping);

            if (mapping.Uri == null)
                throw new ArgumentException(SR.RequiresXmlNamespaceMappingUri, nameof(mapping));

View on GitHub (pinned to 81131a70a4)