dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

FreezableCollection(T).CopyTo(T[] array, int index) throws ArgumentException (SR.Collection_BadRank) when the target array has more than one dimension. CopyTo requires a single-dimensional (rank 1) array so elements can be copied linearly.

Solutions

  1. Pass a one-dimensional array (T[count]) to CopyTo.
  2. Allocate a fresh 1D array and copy from the multidimensional array manually.
  3. Check array.Rank == 1 before calling.

Example fix

// before
var buffer = new Geometry[2, 10];
collection.CopyTo(buffer, 0);
// after
var buffer = new Geometry[collection.Count];
collection.CopyTo(buffer, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array.Rank != 1) throw new InvalidOperationException("CopyTo requires a 1D array");

Try / catch

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

Prevention

When it happens

Trigger: Calling CopyTo with a 2D array such as new T[2,3].

Common situations: Reusing a multidimensional buffer allocated elsewhere; porting code that previously used a different collection allowing multi-rank 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/bbbb35f5fef68cdd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs:481

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