dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

Point3DCollection.ICollection.CopyTo throws ArgumentException(SR.Collection_BadRank) when the destination array is not a one-dimensional (rank 1) array. Multi-dimensional arrays cannot be used as CopyTo targets for these generated WPF collections.

Solutions

  1. Use a one-dimensional array as the CopyTo destination
  2. Copy into a flat array and manually re-index into your 2D structure
  3. Check array.Rank == 1 before calling CopyTo

Example fix

// before
var grid = new Point3D[10, 10];
collection.CopyTo(grid, 0); // throws
// after
var flat = new Point3D[collection.Count];
collection.CopyTo(flat, 0);
for (int i = 0; i < flat.Length; i++) grid[i / 10, i % 10] = flat[i];
Defensive patterns

Strategy: validation

Validate before calling

if (destArray.Rank != 1) throw new ArgumentException("CopyTo requires a rank-1 array");

Type guard

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

Try / catch

try { ((ICollection)col).CopyTo(arr, 0); }
catch (ArgumentException ex) { /* arr.Rank != 1; use flat array */ }

Prevention

When it happens

Trigger: Calling CopyTo(point3dArray2d, index) where point3dArray2d = new Point3D[a,b]; the rank check runs after index validation and throws for Rank != 1.

Common situations: Copying into a rectangular/multidimensional array for grid-style data layouts; interop code reusing 2D buffers.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Point3DCollection.cs:365

        #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)