dotnet/wpf · error

SR.Format(SR.Collection_CopyTo_ArrayCannotBeMultidimensional…

Error message

SR.Format(SR.Collection_CopyTo_ArrayCannotBeMultidimensional)

What it means

The explicit ICollection.CopyTo(Array, int) implementation only supports single-dimensional arrays because it writes DictionaryEntry elements sequentially. If a caller passes a non-null array with Rank != 1 (e.g. a 2D array), it throws ArgumentException with Collection_CopyTo_ArrayCannotBeMultidimensional. The typed CopyTo(DictionaryEntry[], int) is then used for the actual copy.

Solutions

  1. Pass a single-dimensional DictionaryEntry[] (or Array castable to it) sized Count
  2. Convert multidimensional buffers to 1D arrays and copy row-wise yourself
  3. Check array.Rank == 1 before invoking the interface member
  4. Use the typed CopyTo(DictionaryEntry[], int) directly to get clearer errors

Example fix

// before
var grid = new DictionaryEntry[2, 2];
((ICollection)dict).CopyTo(grid, 0); // Rank != 1
// after
var flat = new DictionaryEntry[dict.Count];
((ICollection)dict).CopyTo(flat, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array != null && array.Rank != 1)
    throw new ArgumentException("ICollection.CopyTo requires a single-dimensional array.", nameof(array));

Type guard

bool IsSingleDimensional(System.Array? array) => array == null || array.Rank == 1;

Try / catch

try
{
    ((ICollection)dict).CopyTo(array, index);
}
catch (ArgumentException ex)
{
    // ex.ParamName == "array": switch to a 1D DictionaryEntry[] and retry
}

Prevention

When it happens

Trigger: Calling ((ICollection)dict).CopyTo with a multidimensional array such as new DictionaryEntry[2,2] or casting an object[,] destination; a null array is forwarded to the typed CopyTo and raises ArgumentNullException instead.

Common situations: Interoperating with legacy .NET 1.x collection APIs that use Array, callers reusing an existing multidimensional buffer for a copy, or generic copy helpers that accept Array and assume any rank works.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Localizer/BamlLocalizationDictionary.cs:358

            get
            {
                ArgumentNullException.ThrowIfNull(key);
                return ((IDictionary)_dictionary)[key];
            }
            set
            {
                ArgumentNullException.ThrowIfNull(key);
                ((IDictionary)_dictionary)[key] = value;
            }
        }

        IDictionaryEnumerator IDictionary.GetEnumerator() => GetEnumerator();

        void ICollection.CopyTo(Array array, int index)
        {
            if (array != null && array.Rank != 1)
            {
                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_ArrayCannotBeMultidimensional), nameof(array));
            }

            CopyTo(array as DictionaryEntry[], index);
        }

        int ICollection.Count
        {
            get => Count;
        }

        object ICollection.SyncRoot
        {
            get => ((IDictionary)_dictionary).SyncRoot;
        }

        bool ICollection.IsSynchronized
        {
            get => ((IDictionary)_dictionary).IsSynchronized;

View on GitHub (pinned to 81131a70a4)