dotnet/wpf · error · ArgumentException

Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength

Error message

Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength

What it means

ThousandthOfEmRealDoubles.CopyTo throws ArgumentException(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength) when arrayIndex >= array.Length. There is no room at the given start index, so the standard collection contract requires this error.

Solutions

  1. Pass arrayIndex strictly less than array.Length (start at 0 for a fresh buffer)
  2. Validate with: if (arrayIndex >= array.Length) fix the index before calling
  3. Track buffer occupancy explicitly so the next CopyTo offset is computed correctly

Example fix

// before
int offset = itemsCopied; // e.g. equals dest.Length
collection.CopyTo(dest, offset);
// after
if (offset < dest.Length) collection.CopyTo(dest, offset);
Defensive patterns

Strategy: validation

Validate before calling

if (index >= dest.Length)
    throw new ArgumentException("index must be < dest.Length");
collection.CopyTo(dest, index);

Prevention

When it happens

Trigger: Calling CopyTo with an arrayIndex equal to or larger than the destination array length, e.g. CopyTo(new double[10], 10) or CopyTo(new double[10], 50).

Common situations: Off-by-one indexing (using array.Length instead of a computed free position); reusing an index computed for a larger buffer; copying into a shrunk buffer.

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/83ceb938d8bb21b5. Report an issue: GitHub.

Appendix: source

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

        }

        public void CopyTo(double[] array, int arrayIndex)
        {
            // parameter validations
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
            {
                throw new ArgumentException(
                    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

View on GitHub (pinned to 81131a70a4)