dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

This SR resource backs an ArgumentException thrown by the generated ICollection.CopyTo(Array, int) implementation (CollectionHelper.cs, Collection_SetValue) when the destination array has Rank != 1. CopyTo only supports single-dimensional destination arrays.

Solutions

  1. Pass a single-dimensional array of sufficient length: new T[count]
  2. Validate array.Rank == 1 before calling CopyTo
  3. If you need 2D storage, copy into a 1D array and repack manually
  4. Catch ArgumentException and give callers a clear message about array rank

Example fix

// before
collection.CopyTo(new object[rows, cols], 0); // BadRank
// after
var flat = new object[collection.Count];
collection.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-D", nameof(array));
if (array.Length - index < collection.Count) throw new ArgumentException("array too small");

Try / catch

try { collection.CopyTo(array, index); }
catch (ArgumentException ex) { throw new ArgumentException("CopyTo requires a 1-D array", ex); }

Prevention

When it happens

Trigger: Calling CopyTo on a generated collection passing a multidimensional array (e.g. new object[2,2]) as the destination.

Common situations: Reusing a rectangular/jagged array allocated elsewhere; generic code that accepts Array and builds arrays of arbitrary rank; legacy interop code using 2D buffers.

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/ca0eba7afe58d420. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/helpers/CollectionHelper.cs:369

                    {
                        [[WriteCopyToCommon(resource)]]

                        _collection.CopyTo(array, index);
                    }
                [[/inline]];
        }

        public static string WriteCopyToArray(McgResource resource)
        {
            return
                [[inline]]
                    void ICollection.CopyTo(Array array, int index)
                    {
                        [[WriteCopyToCommon(resource)]]

                        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);
                        }
                    }
                [[/inline]];

View on GitHub (pinned to 81131a70a4)