dotnet/wpf · error · ArgumentException

Collection_CopyTo_NumberOfElementsExceedsArrayLength

Error message

Collection_CopyTo_NumberOfElementsExceedsArrayLength

What it means

ThousandthOfEmRealDoubles.CopyTo throws ArgumentException(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength) when (array.Length - Count - arrayIndex) < 0, i.e. the destination array lacks space for Count elements starting at arrayIndex. This is the standard ICollection.CopyTo capacity contract.

Solutions

  1. Allocate the destination with at least collection.Count + arrayIndex elements: new double[collection.Count]
  2. Check dest.Length - arrayIndex >= collection.Count before calling
  3. Copy to offset 0 with a correctly sized buffer to avoid double-counting the index

Example fix

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

Strategy: validation

Validate before calling

if (dest.Length - index < collection.Count)
    throw new ArgumentException("dest too small");
collection.CopyTo(dest, index);

Prevention

When it happens

Trigger: Calling CopyTo with a destination array whose available space (Length - arrayIndex) is smaller than the collection's Count, e.g. CopyTo(new double[3], 0) on a collection with 5 items.

Common situations: Sizing the buffer from a stale Count (collection changed since sizing); forgetting to account for a non-zero arrayIndex offset; under-allocating by using array length of the source instead of Count.

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/5cebe987ef3b5cb4. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/ThousandthOfEmRealDoubles.cs:188

                    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<double> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {

View on GitHub (pinned to 81131a70a4)