dotnet/wpf · error · ArgumentException

SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArra…

Error message

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

What it means

CopyTo checks that the starting index is within the destination array: if index >= array.Length there is no room for any element, so CopyItems throws ArgumentException with Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength.

Solutions

  1. Ensure index < array.Length before calling CopyTo.
  2. Size the destination array as collection.Count + index.
  3. Use Array.Copy semantics or ToArray() when copying the whole collection from position 0.

Example fix

// before
var dest = new FamilyTypeface[collection.Count];
collection.CopyTo(dest, collection.Count); // ArgumentException
// after
var dest = new FamilyTypeface[collection.Count * 2];
collection.CopyTo(dest, collection.Count);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || arrayIndex >= array.Length) throw new ArgumentException("index must be less than array.Length");
if (collection.Count > array.Length - arrayIndex) throw new ArgumentException("array too small");

Try / catch

try { collection.CopyTo(array, arrayIndex); }
catch (ArgumentException ex) when (ex.Message.Contains("IndexGreaterThanOrEqualToArrayLength")) { /* adjust index or array size */ }

Prevention

When it happens

Trigger: Calling CopyTo(array, index) with an index equal to or larger than array.Length — e.g. CopyTo(arr, arr.Length), or a computed offset that exceeds the array bounds.

Common situations: Appending multiple collections into one array where accumulated offsets exceed the array length, or copying into an empty array with a nonzero index.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyTypefaceCollection.cs:376

            if (familyTypeface == null)
                throw new ArgumentException(SR.Format(SR.CannotConvertType, obj.GetType(), typeof(FamilyTypeface)));

            return familyTypeface;
        }

        private void CopyItems(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));

            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)
            {
                InitializeItemsFromInnerList();
                Array.Copy(_items, 0, array, index, _count);
            }
        }

        private class Enumerator : IEnumerator<FamilyTypeface>, SC.IEnumerator
        {
            private FamilyTypefaceCollection _list;
            private int _index;
            private FamilyTypeface _current;

            internal Enumerator(FamilyTypefaceCollection list)

View on GitHub (pinned to 81131a70a4)