dotnet/wpf · error · ArgumentException

SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…

Error message

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

What it means

FamilyMapCollection.CopyTo(FontFamilyMap[] array, int index) throws ArgumentException when index is greater than or equal to array.Length, because there is no room for even one element at that position. This mirrors the standard ICollection.CopyTo contract.

Solutions

  1. Pass an index within [0, array.Length-1], typically 0 for a fresh buffer.
  2. Size the destination array to at least index + collection.Count before copying.
  3. Guard the call: if (index < array.Length) collection.CopyTo(array, index);

Example fix

// before
var maps = new FontFamilyMap[0];
familyMaps.CopyTo(maps, 0);
// after
var maps = new FontFamilyMap[familyMaps.Count];
familyMaps.CopyTo(maps, 0);
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try { col.CopyTo(array, index); }
catch (ArgumentException ex) { log.Warn($"CopyTo bounds: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling CopyTo with an index equal to or beyond the destination array length, e.g. copying into an empty array with index 0, or reusing a stale index from a larger array.

Common situations: Paginated copy loops that overshoot the array end, buffers sized from a previous collection count, or passing collection Count as the index.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs:77

        }

        /// <summary>
        /// Determines whether the FontFamily contains the specified FontFamilyMap.
        /// </summary>
        public bool Contains(FontFamilyMap item)
        {
            return FindItem(item) >= 0;
        }

        /// <summary>
        /// Copies the contents of the list to the specified array.
        /// </summary>
        public void CopyTo(FontFamilyMap[] array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);

            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"));

            if (_count != 0)
                Array.Copy(_items, 0, array, index, _count);
        }

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

            if (array.Rank != 1)
                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);

            Type elementType = array.GetType().GetElementType();
            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))
                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType));

View on GitHub (pinned to 81131a70a4)