dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

DoubleCollection.CopyTo throws ArgumentException when the destination array is multi-dimensional (Rank != 1). ICollection<T>.CopyTo requires a single-dimensional array with zero-based indexing, and WPF enforces this with SR.Collection_BadRank after validating the index range.

Solutions

  1. Pass a single-dimensional double[] sized to Count plus the starting index.
  2. Flatten the multidimensional array first or copy element-by-element.
  3. Validate array.Rank == 1 before calling CopyTo.
  4. Use LINQ ToArray() to obtain a 1D array from the collection instead.

Example fix

// before
var rect = new double[points.Count, 2];
doubleCollection.CopyTo(rect, 0); // ArgumentException: rank
// after
var flat = new double[points.Count];
doubleCollection.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-dimensional");
if (index < 0 || index > array.Length - collection.Count) throw new ArgumentOutOfRangeException(nameof(index));

Type guard

bool isValidTarget = array is double[] oneDim && index >= 0 && index + collection.Count <= oneDim.Length;

Try / catch

try { collection.CopyTo(array, index); } catch (ArgumentException) { /* array was multidimensional; copy element-wise */ for (int i = 0; i < collection.Count; i++) flat[index + i] = collection[i]; }

Prevention

When it happens

Trigger: Calling DoubleCollection.CopyTo(array, index) where array.Rank != 1, e.g. a 2D double[,] array; also index < 0 or index > array.Length - Count (those throw ArgumentOutOfRangeException first).

Common situations: Copying a DoubleCollection (points, stroke dash data) into a rectangular 2D array assumed to be flattenable; passing buffers from legacy code shaped as multidimensional 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/6ec9a39e8d9e9784. Report an issue: GitHub.

Appendix: source

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