dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

Transform3DCollection.CopyTo(Array array, int index) throws ArgumentException (SR.Collection_BadRank) when the destination array is not a one-dimensional array. ICollection.CopyTo only supports flat arrays because SetValue is used with a linear index.

Solutions

  1. Pass a one-dimensional array, e.g. new Transform3D[collection.Count].
  2. If you need a 2D layout, copy to a flat array first and then reshuffle manually.
  3. Guard with array.Rank == 1 before calling CopyTo.

Example fix

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

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("Destination array must be 1-D", nameof(array));

Type guard

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

Try / catch

try { collection.CopyTo(arr, 0); } catch (ArgumentException) { /* recreate as 1-D array and retry */ }

Prevention

When it happens

Trigger: Passing a multidimensional array (e.g. new Transform3D[2,2]) to CopyTo with a valid index.

Common situations: Copying into a reusable 2D buffer; generic serialization or interop code that hands the collection a rectangular array.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Transform3DCollection.cs:412

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