dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

TextEffectCollection.CopyTo throws ArgumentException(Collection_BadRank) when the destination array has Rank != 1, i.e. it is multidimensional (e.g. TextEffect[_,_]). WPF collections only support copying into single-dimensional arrays, consistent with other BCL collection implementations (per the Windows 1587365 note). The bounds checks (negative index, index + count beyond Length) run first and throw ArgumentOutOfRangeException separately.

Solutions

  1. Use a single-dimensional array: new TextEffect[collection.Count].
  2. Flatten a multidimensional array first (or copy into a flat temp array then reshape).
  3. Check array.Rank == 1 before calling CopyTo.

Example fix

// before
var dest = new TextEffect[2, 2];
collection.CopyTo(dest, 0); // ArgumentException Collection_BadRank
// after
var dest = new TextEffect[collection.Count];
collection.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

static bool CanCopyTo(Array dest, int index, int count) => dest != null && dest.Rank == 1 && index >= 0 && index + count <= dest.Length;

Type guard

static bool IsSingleDimensional(Array a) => a?.Rank == 1;

Try / catch

try { collection.CopyTo(dest, index); }
catch (ArgumentException ex) when (ex.Message.Contains("Collection_BadRank")) { /* recreate dest as rank-1 array */ }

Prevention

When it happens

Trigger: Calling CopyTo with a 2D (or higher) array such as new TextEffect[2,2], or an Array-typed variable that happens to be multidimensional; note index out of bounds on a 1D array throws ArgumentOutOfRangeException before the rank check.

Common situations: Generic copy helpers written against Array that receive rectangular arrays; legacy code copying into matrices; APIs that allocate arrays dynamically and produce rank-2 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/55dcd13fa73c80b6. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/TextEffectCollection.cs:402

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