dotnet/wpf · error · ArgumentException

Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength

Error message

Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength

What it means

This entry covers ThousandthOfEmRealPoints.CopyTo throwing ArgumentException(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength) when arrayIndex >= array.Length (the source line:178 region shows the same validation as ThousandthOfEmRealDoubles:178; the multidimensional check at line 114 precedes it). It enforces the ICollection.CopyTo contract for the Point-based fixed-size metric collection.

Solutions

  1. Pass arrayIndex strictly less than array.Length (0 for a fresh buffer)
  2. Use a single-dimensional Point[] with Length >= Count + arrayIndex
  3. Pre-validate: ArgumentNullException.ThrowIfNull(dest); if (dest.Rank != 1 || index >= dest.Length) fix before calling

Example fix

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

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(dest);
if (dest.Rank != 1) throw new ArgumentException("dest must be 1-D");
if (index >= dest.Length) throw new ArgumentException("index out of range");
if (dest.Length - index < collection.Count) throw new ArgumentException("dest too small");
collection.CopyTo(dest, index);

Prevention

When it happens

Trigger: Calling CopyTo on ThousandthOfEmRealPoints with arrayIndex >= destination array length, e.g. CopyTo(new Point[4], 4); also throws the multidimensional error at line 114 if the destination Array has Rank != 1 (e.g. Point[,]).

Common situations: Off-by-one offsets into shared metric buffers; passing 2D Point arrays; reusing indices computed for a different (larger) buffer; copying glyph-origin data with stale bounds.

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

Appendix: source

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

        }

        public void CopyTo(Point[] 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)