dotnet/wpf · error · ArgumentException

SR.Arg_RankMultiDimNotSupported

Error message

SR.Arg_RankMultiDimNotSupported

What it means

ICollection.CopyTo requires a single-dimensional, zero-based array; the library throws ArgumentException with SR.Arg_RankMultiDimNotSupported when array.Rank != 1. Multi-dimensional arrays cannot be indexed with a single flat offset, so the copy would be ambiguous.

Solutions

  1. Pass a single-dimensional zero-based array (T[] or object[])
  2. Copy into a flat 1D array and translate to the 2D target manually
  3. Guard with array.Rank == 1 before calling CopyTo

Example fix

// before
var grid = new WeakReference[count, count];
collection.CopyTo(grid, 0);
// after
var flat = new WeakReference[count * 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("CopyTo requires a single-dimensional array.", nameof(array));

Type guard

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

Try / catch

try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException e) when (e.Message.Contains("Rank")) { /* flatten to 1D and retry */ }

Prevention

When it happens

Trigger: Calling CopyTo on WeakReadOnlyCollection (explicit ICollection.CopyTo) passing a 2D array like new WeakReference[3,3] or any array with Rank > 1.

Common situations: Legacy .NET code or interop scenarios that still use rectangular arrays; passing a buffer created as T[,] for chunked copying.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs:141

                    if (list is ICollection c)
                    {
                        _syncRoot = c.SyncRoot;
                    }
                    else
                    {
                        System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
                    }
                }
                return _syncRoot;
            }
        }

        void ICollection.CopyTo(Array array, int index) {
            ArgumentNullException.ThrowIfNull(array);

            if (array.Rank != 1) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
            }

            if( array.GetLowerBound(0) != 0 ) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
                throw new ArgumentException(SR.Arg_NonZeroLowerBound);
            }

            if (index < 0) {
                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (array.Length - index < Count) {
                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);
            }

            IList<T> dlist = CreateDereferencedList();

View on GitHub (pinned to 81131a70a4)