dotnet/wpf · error

SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayL…

Error message

SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, "arrayIndex", "array")

What it means

CopyTo throws this ArgumentException when the dictionary has more elements than fit in the destination array starting at arrayIndex, i.e. Count > array.Length - arrayIndex. The full set of entries could not be copied without overflowing the array, so the library refuses the operation up front.

Solutions

  1. Size the destination array to exactly dict.Count (or more) before copying
  2. Account for arrayIndex in the size: array.Length must be >= Count + arrayIndex
  3. Re-read Count immediately before CopyTo if the dictionary can change
  4. Use a List<DictionaryEntry> instead if a fixed buffer is not required

Example fix

// before
var entries = new DictionaryEntry[2];
dict.CopyTo(entries, 0); // Count > array.Length
// after
var entries = new DictionaryEntry[dict.Count];
dict.CopyTo(entries, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (dict.Count > array.Length - arrayIndex)
    throw new ArgumentException($"Destination too small: need {dict.Count} slots from index {arrayIndex}, array has {array.Length}.");

Type guard

bool FitsDestination<T>(T[] array, int arrayIndex, int count) => array != null && arrayIndex >= 0 && array.Length - arrayIndex >= count;

Prevention

When it happens

Trigger: Calling BamlLocalizationDictionary.CopyTo with an array whose usable space (array.Length - arrayIndex) is smaller than the dictionary's Count, e.g. CopyTo(new DictionaryEntry[2], 0) when the dictionary holds 5 localizable resources.

Common situations: Sizing the destination array from a stale or cached count after the dictionary changed, allocating a buffer for a subset but copying everything, or passing a large arrayIndex into a barely-large-enough array.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs:305

        }

        /// <summary>
        ///     Copies the dictionary's elements to a one-dimensional 
        ///     Array instance at the specified index.
        /// </summary>
        public void CopyTo(DictionaryEntry[] array, int arrayIndex)
        {
            ArgumentNullException.ThrowIfNull(array);
            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);

            if (arrayIndex >= array.Length)
            {
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "arrayIndex", "array"), nameof(arrayIndex));
            }

            if (Count > (array.Length - arrayIndex))
            {
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, "arrayIndex", "array"));
            }

            foreach (KeyValuePair<BamlLocalizableResourceKey, BamlLocalizableResource> pair in _dictionary)
            {
                DictionaryEntry entry = new(pair.Key, pair.Value);
                array[arrayIndex++] = entry;
            }
        }

        #region interface ICollection, IEnumerable, IDictionary
        //------------------------------
        // interface functions
        //------------------------------      

        bool IDictionary.Contains(object key)
        {
            ArgumentNullException.ThrowIfNull(key);
            return ((IDictionary)_dictionary).Contains(key);

View on GitHub (pinned to 81131a70a4)