dotnet/wpf · error · ArgumentException

SR.GridCollection_DestArrayInvalidRank

Error message

SR.GridCollection_DestArrayInvalidRank

What it means

The explicit ICollection.CopyTo implementation on ColumnDefinitionCollection requires the destination Array to be single-dimensional (Rank == 1). Passing a multidimensional array (Rank > 1) throws ArgumentException with SR.GridCollection_DestArrayInvalidRank, because the collection can only copy into a flat array.

Solutions

  1. Pass a one-dimensional array, e.g. new ColumnDefinition[grid.ColumnDefinitions.Count] or object[...].
  2. Copy element-by-element into the multidimensional array with a for loop over the indices.
  3. Use LINQ: grid.ColumnDefinitions.Cast<object>().ToArray() to get a flat array.

Example fix

// before
var arr = new object[defs.Count, 2];
((ICollection)defs).CopyTo(arr, 0);
// after
var arr = new object[defs.Count];
((ICollection)defs).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 1-D", nameof(array));

Type guard

static bool IsFlatArray(Array a) => a != null && a.Rank == 1;

Try / catch

try { ((ICollection)defs).CopyTo(array, 0); }
catch (ArgumentException ex) { log.Error("CopyTo requires a single-dimensional array", ex); }

Prevention

When it happens

Trigger: Calling ((ICollection)grid.ColumnDefinitions).CopyTo(multiDimArray, 0) where multiDimArray was created with ranks > 1 (e.g. new object[2,2]).

Common situations: Copying grid definitions into a shared 2D scratch array; generic serialization code that allocates multidimensional buffers and passes them to ICollection.CopyTo.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ColumnDefinition.cs:78

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public Methods

        /// <summary>
        ///     <see cref="ICollection.CopyTo"/>
        /// </summary>
        void ICollection.CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);
            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.GridCollection_DestArrayInvalidRank);
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException(SR.Format(SR.GridCollection_DestArrayInvalidLowerBound, "index"));
            }
            if (array.Length - index < _size)
            {
                throw new ArgumentException(SR.Format(SR.GridCollection_DestArrayInvalidLength, "array"));
            }

            if (_size > 0)
            {
                Debug.Assert(_items != null);
                Array.Copy(_items, 0, array, index, _size);
            }
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)