dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

ArgumentException thrown by VisualCollection.CopyTo when the target array's Rank is not 1 — the copy helper only supports single-dimensional arrays, so a multidimensional destination array is rejected even if it is large enough.

Solutions

  1. Pass a one-dimensional Visual[] (or Array) sized at least count + index.
  2. If data is inherently 2D, copy into a flat array first and reshape yourself.
  3. Validate array.Rank == 1 before calling to fail fast with a clearer message.

Example fix

// before
var grid = new Visual[rows, cols];
visualCollection.CopyTo(grid, 0); // ArgumentException
// after
var flat = new Visual[visualCollection.Count];
visualCollection.CopyTo(flat, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (targetArray.Rank != 1 || targetArray.Length - index < visualCollection.Count)
    throw new ArgumentException("CopyTo requires a 1-D array with enough room");
visualCollection.CopyTo(targetArray, index);

Type guard

static bool CanCopyTo(Array a, int index, int count) => a.Rank == 1 && a.Length - index >= count;

Try / catch

try { collection.CopyTo(arr, 0); }
catch (ArgumentException) { /* wrong array rank or size */ }

Prevention

When it happens

Trigger: Calling CopyTo with a 2D array, e.g. visuals.CopyTo(new Visual[2,5], 0), or any array created with more than one dimension.

Common situations: Implementing generic serialization/dumping code that passes a rect array or matrix array to CopyTo; misunderstanding that 'array' overloads accept only flat 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/4720b91aa63957ec. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/VisualCollection.cs:174

            {
                VerifyAPIReadOnly();

                return this;
            }
        }

        /// <summary>
        /// Copies the Visual collection to the specified array starting at the specified index.
        /// </summary>
        public void CopyTo(Array array, int index)
        {
            VerifyAPIReadOnly();

            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.Collection_BadRank);
            }

            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _size);

            // System.Array does not have a CopyTo method that takes a count. Therefore
            // the loop is programmed here out.
            for (int i=0; i < _size; i++)
            {
                array.SetValue(_items[i], i+index);
            }
}

        /// <summary>
        /// Copies the Visual collection to the specified array starting at the specified index.
        /// </summary>
        public void CopyTo(Visual[] array, int index)
        {

View on GitHub (pinned to 81131a70a4)