dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

TextDecorationCollection.CopyTo throws ArgumentException (SR.Collection_BadRank) when the destination array has Rank != 1. CopyTo only supports single-dimensional (SZ) arrays, matching BCL collection conventions. The index bounds are already validated before this check.

Solutions

  1. Pass a single-dimensional array, e.g. new TextDecoration[count]
  2. If you must use a multidimensional array, copy to a flat array first and then re-index manually
  3. Fix array allocation at the call site to use rank-1 arrays

Example fix

// before
var arr = new TextDecoration[count, 1];
collection.CopyTo(arr, 0);
// after
var arr = new TextDecoration[count];
collection.CopyTo(arr, 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 single-dimensional", nameof(array));
if (index < 0 || index + collection.Count > array.Length) throw new ArgumentException("Index out of range");

Type guard

bool CanCopyTo(Array array) => array != null && array.Rank == 1 && array.Length - index >= collection.Count;

Prevention

When it happens

Trigger: Passing a 2D array (e.g. TextDecoration[,] or Array.CreateInstance with rank 2) as the destination of CopyTo.

Common situations: Generic array-copy helper code that allocates multidimensional arrays; legacy code ported from ArrayList-era APIs that tolerated 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/84f3606fe30a6368. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs:393

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