dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

DrawingCollection's ICollection.CopyTo validates the destination Array's rank and throws ArgumentException with SR.Collection_BadRank when array.Rank != 1 (a multidimensional array). The BCL collection contract only supports copying into single-dimensional, zero-based arrays.

Solutions

  1. Pass a single-dimensional Drawing[] (or compatible element-type 1D array) to CopyTo.
  2. Copy manually into the multidimensional array with a foreach loop and explicit index math.
  3. Declare the destination buffer as a flat 1D array and compute offsets yourself.

Example fix

// before
var dest = new Drawing[rows, cols];
((ICollection)collection).CopyTo(dest, 0); // ArgumentException: Collection_BadRank
// after
var dest = new Drawing[collection.Count];
collection.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Rank != 1)
    throw new ArgumentException("CopyTo requires a single-dimensional array.", nameof(array));
((ICollection)coll).CopyTo(array, index);

Type guard

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

Try / catch

try { ((ICollection)coll).CopyTo(array, index); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadRank"))
{ /* allocate a 1D Drawing[] and copy manually */ }

Prevention

When it happens

Trigger: Calling ((ICollection)drawingCollection).CopyTo(array, index) where array is a 2D (or higher-rank) Array, e.g. new Drawing[n, m].

Common situations: Reusing a multidimensional scratch buffer as a copy destination; generic array-copy helpers that accept Array and pass through whatever rank the caller supplies; legacy code assuming rank flexibility.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs:414

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