dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

PointCollection.CopyTo throws Collection_BadRank (ArgumentException) when the destination array has a rank other than 1 (multi-dimensional array). WPF collection CopyTo implementations only support single-dimensional arrays, matching BCL conventions.

Solutions

  1. Use a one-dimensional array: new Point[collection.Count].
  2. Copy via ToArray() or the generic ICollection<Point>.CopyTo.
  3. If a 2D layout is needed, copy into a flat array and index it manually.
  4. Validate array.Rank == 1 before calling CopyTo in generic helpers.

Example fix

// before
var grid = new Point[2, 2];
points.CopyTo(grid, 0); // throws
// after
var flat = new Point[points.Count];
points.CopyTo(flat, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || array.Rank != 1 || index < 0 || index + points.Count > array.Length)
    throw new ArgumentException("Destination must be a 1-D array with sufficient capacity");

Type guard

static bool IsFlatArray(Array a) => a.Rank == 1;

Try / catch

try { points.CopyTo(arr, 0); }
catch (ArgumentException ex) when (ex.Message.Contains("rank")) { /* use 1-D array */ }

Prevention

When it happens

Trigger: Calling CopyTo on a PointCollection passing a multidimensional array, e.g. new Point[2,2], as the destination.

Common situations: Legacy COM/WinForms-era code using rectangular arrays, ported code assuming multidimensional copy support, or generic array helpers that accept Array and allocate by dimensions.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/PointCollection.cs:367

        #endregion

        #region ICollection

        void ICollection.CopyTo(Array array, int index)
        {
            ReadPreamble();

            ArgumentNullException.ThrowIfNull(array);

            // This will not throw in the case that we are copying
            // from an empty collection.  This is consistent with the
            // BCL Collection implementations. (Windows 1587365)
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);

            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.Collection_BadRank);
            }

            // Elsewhere in the collection we throw an AE when the type is
            // bad so we do it here as well to be consistent
            try
            {
                int count = _collection.Count;
                for (int i = 0; i < count; i++)
                {
                    array.SetValue(_collection[i], index + i);
                }
            }
            catch (InvalidCastException e)
            {
                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);
            }
        }

View on GitHub (pinned to 81131a70a4)