dotnet/wpf · error · ArgumentException

Collection_CopyTo_NumberOfElementsExceedsArrayLength

Error message

Collection_CopyTo_NumberOfElementsExceedsArrayLength

What it means

Thrown by ThousandthOfEmRealPoints.CopyTo when the destination array does not have room for all collection elements starting at arrayIndex. It is a standard ArgumentException guarding the ICollection.CopyTo contract, analogous to copying a Point[] into a too-small target.

Solutions

  1. Size the destination array to at least collection.Count + arrayIndex before copying (e.g. Array.Resize or allocate new Point[count]).
  2. Check array.Length - arrayIndex >= collection.Count before calling CopyTo.
  3. Verify arrayIndex is >= 0 and within array bounds; a stale offset is a frequent cause.

Example fix

// before
var points = new Point[oldCount];
collection.CopyTo(points, 0);
// after
var points = new Point[collection.Count];
collection.CopyTo(points, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0 || arrayIndex > array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (array.Length - arrayIndex < collection.Count)
    throw new ArgumentException("Destination array too small", nameof(array));

Try / catch

try { collection.CopyTo(arr, idx); }
catch (ArgumentException) { arr = new Point[collection.Count]; collection.CopyTo(arr, 0); }

Prevention

When it happens

Trigger: Calling CopyTo(array, arrayIndex) where array.Length - Count - arrayIndex < 0, i.e. the remaining space after arrayIndex is smaller than the collection's Count, or arrayIndex itself is beyond the array bounds.

Common situations: Marshalling a glyph-run/advance-width Point collection into a pre-allocated buffer sized for a different text run (font size or character count changed), or copying with an offset index into an array reused from a previous, larger run.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealPoints.cs:133

                    SR.Collection_CopyTo_ArrayCannotBeMultidimensional, 
                    nameof(array));                
            }

            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);

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

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

            // do the copying here
            for (int i = 0; i < Count; i++)
            {
                array[arrayIndex + i] = this[i];
            }
        }

        public IEnumerator<Point> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {

View on GitHub (pinned to 81131a70a4)