dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

Model3DCollection.CopyTo(array, index) requires a single-dimensional (rank-1) destination array and throws ArgumentException(SR.Collection_BadRank) otherwise. CopyTo uses Array.SetValue on the target, which cannot address multi-dimensional arrays with a linear index. The BCL convention for ICollection.CopyTo is to reject multidimensional arrays.

Solutions

  1. Pass a one-dimensional Model3D[] of length >= Count + index.
  2. Allocate a new Model3D[collection.Count] for the copy.
  3. Refactor surrounding code that stores models in multidimensional arrays to use jagged arrays or lists.

Example fix

// before
var target = new Model3D[2, 2];
collection.CopyTo(target, 0); // ArgumentException: bad rank
// after
var target = new Model3D[collection.Count];
collection.CopyTo(target, 0);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool CanCopyTo(Array a, int i) => a != null && a.Rank == 1 && i >= 0 && a.Length - i >= collection.Count;

Try / catch

try { collection.CopyTo(array, index); } catch (ArgumentException ex) { // bad rank or bad dest array
    var dest = new Model3D[collection.Count]; collection.CopyTo(dest, 0); }

Prevention

When it happens

Trigger: Calling CopyTo on a Model3DCollection with a 2D (or higher) array as the destination; also possible if index > array.Length - Count, though that throws ArgumentOutOfRangeException first.

Common situations: Reusing a rectangular Model3D[,] buffer used elsewhere; generic serialization helpers that accept arbitrary arrays.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Model3DCollection.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)