dotnet/wpf · error · ArgumentException

SR.BadTargetArray

Error message

SR.BadTargetArray

What it means

ItemCollection.CopyTo throws ArgumentException(SR.BadTargetArray) when the destination Array has more than one dimension. .NET copy operations require a single-dimensional (SZ) array so elements can be laid out sequentially. The library rejects multidimensional arrays up front before touching the collection view.

Solutions

  1. Pass a single-dimensional array such as new object[itemsControl.Items.Count].
  2. If you need 2D layout, copy into a flat 1D array first and then map indices yourself.
  3. Guard with array.Rank == 1 before calling CopyTo.

Example fix

// before
var arr = new object[items.Count, 2];
items.CopyTo(arr, 0);
// after
var arr = new object[items.Count];
items.CopyTo(arr, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || array.Rank != 1)
    throw new ArgumentException("Destination must be a single-dimensional array", nameof(array));
items.CopyTo(array, 0);

Type guard

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

Try / catch

try { items.CopyTo(array, 0); }
catch (ArgumentException ex) when (ex.ParamName == "array") { /* use a 1D array */ }

Prevention

When it happens

Trigger: Calling ItemCollection.CopyTo(Array array, int index) with an array created as e.g. new object[2,3] (Rank > 1).

Common situations: Reusing a legacy 2D buffer or a rectangular array from older code to collect ItemsControl items; interop code that builds grid-like arrays and assumes CopyTo accepts any Array.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:286

            return _collectionView.Contains(containItem);
        }

        /// <summary>
        ///     Makes a shallow copy of object references from this
        ///     ItemCollection to the given target array
        /// </summary>
        /// <param name="array">
        ///     Target of the copy operation
        /// </param>
        /// <param name="index">
        ///     Zero-based index at which the copy begins
        /// </param>
        public void CopyTo(Array array, int index)
        {
            ArgumentNullException.ThrowIfNull(array);
            if (array.Rank > 1)
                throw new ArgumentException(SR.BadTargetArray, nameof(array)); // array is multidimensional.
            ArgumentOutOfRangeException.ThrowIfNegative(index);

            // use the view instead of the collection, because it may have special sort/filter
            if (!EnsureCollectionView())
                return;  // there is no collection (bind returned no collection) and therefore nothing to copy

            VerifyRefreshNotDeferred();

            IndexedEnumerable.CopyTo(_collectionView, array, index);
        }

        /// <summary>
        ///     Finds the index in this collection/view where the given item is found.
        /// </summary>
        /// <param name="item">
        ///     The item whose index in this collection/view is to be retrieved.
        /// </param>
        /// <returns>

View on GitHub (pinned to 81131a70a4)