dotnet/wpf · error · ArgumentException

SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…

Error message

SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array")

What it means

The ICollection.CopyTo(Array array, int index) overload of CharacterMetricsDictionary validates that index is within the destination array: if index >= array.Length there is no room for even one element, so an ArgumentException with Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength is thrown.

Solutions

  1. Ensure 0 <= index < array.Length.
  2. Ensure array.Length >= index + dictionary.Count (the next check enforces room for all elements).
  3. Guard with `if (index < array.Length)` or copy into a right-sized array at index 0.

Example fix

// before
((ICollection)dict).CopyTo(entries, entries.Length);
// after
((ICollection)dict).CopyTo(entries, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0 || index >= array.Length)
    throw new ArgumentOutOfRangeException(nameof(index));

Try / catch

try { ((ICollection)dict).CopyTo(array, index); }
catch (ArgumentException ex) { /* correct index or array and retry */ }

Prevention

When it happens

Trigger: Calling ((ICollection)dictionary).CopyTo(array, index) with index >= array.Length on an Array (including DictionaryEntry[] or object[] targets).

Common situations: Interop with non-generic ICollection consumers passing a start offset computed elsewhere; copying into the tail of a shared buffer with a wrong offset; empty array with index 0.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:178

        bool SC.ICollection.IsSynchronized
        {
            get { return false; }
        }

        object SC.ICollection.SyncRoot
        {
            get { return this; }
        }

        void SC.ICollection.CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);

            ArgumentOutOfRangeException.ThrowIfNegative(index);

            if (index >= array.Length)
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, "index", "array"));

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

            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];
            if (typedArray != null)
            {
                // it's an array of the exact type
                foreach (KeyValuePair<int, CharacterMetrics> item in this)
                {
                    typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);
                }
            }
            else
            {
                // it's an array of some other type, e.g., object[]; make sure it's one dimensional
                if (array.Rank != 1)
                    throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);

View on GitHub (pinned to 81131a70a4)