dotnet/wpf · error · ArgumentException

Cannot pass multidimensional array to the CopyTo method on…

Error message

Cannot pass multidimensional array to the CopyTo method on a collection.

What it means

PartialArray<T>.CopyTo validates the destination array and throws ArgumentException when the array's Rank is not 1, i.e. a multidimensional array (e.g. T[,]) was passed. ICollection<T>.CopyTo only supports single-dimensional arrays.

Solutions

  1. Pass a single-dimensional array (T[]) as the destination.
  2. If a 2D layout is needed, copy to a flat T[] first and map indices manually.
  3. Add a Rank check before calling CopyTo in generic helper code.

Example fix

// before
var grid = new T[n, m];
partialArray.CopyTo(grid, 0);
// after
var flat = new T[partialArray.Count];
partialArray.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("Single-dimensional array required", nameof(array));

Type guard

bool IsSingleDimensionalArray(Array a) => a.Rank == 1;

Try / catch

try { coll.CopyTo(dest, 0); }
catch (ArgumentException) { dest = new T[coll.Count]; coll.CopyTo(dest, 0); }

Prevention

When it happens

Trigger: Calling CopyTo with a 2D+ array such as new T[10,10], whether directly or via ICollection<T>.CopyTo/serialization code.

Common situations: Copying collection contents into a grid/matrix-shaped buffer, or reflection-based copy routines that pass through whatever array was at hand.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/PartialArray.cs:128

        #region ICollection<T> Members

        public int Count
        {
            get
            {
                return _count;
            }
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            // parameter validations
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1)
            {
                throw new ArgumentException(
                    SR.Collection_CopyTo_ArrayCannotBeMultidimensional, 
                    nameof(array));                
            }

            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);

            if (arrayIndex >= array.Length)
            {
                throw new ArgumentException(
                    SR.Format(
                        SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, 
                        "arrayIndex", 
                        "array"),
                        nameof(arrayIndex));
            }

            if ((array.Length - Count - arrayIndex) < 0)
            {

View on GitHub (pinned to 81131a70a4)