dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

IntMap.CopyTo validates the destination array like any ICollection.CopyTo implementation: the array must be non-null, exactly one-dimensional (Rank == 1), and the startIndex must be in range. A multi-dimensional array is rejected with ArgumentException(SR.Collection_BadRank).

Solutions

  1. Use a one-dimensional array: new KeyValuePair<int,ushort>[intMap.Count].
  2. Check array.Rank == 1 before calling CopyTo.
  3. Flatten multi-dimensional data into a linear array and use arrayIndex offsets.

Example fix

// before
var dest = new KeyValuePair<int, ushort>[10, 10];
intMap.CopyTo(dest, 0); // throws Collection_BadRank
// after
var dest = new KeyValuePair<int, ushort>[intMap.Count];
intMap.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("Destination must be a 1-D array.", nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex));

Type guard

bool CanCopyTo<T>(T[] array, int index) => array != null && array.Rank == 1 && index >= 0 && index < array.Length;

Prevention

When it happens

Trigger: Calling IntMap.CopyTo(some2DArray, 0) where the target array was created with more than one dimension (e.g. new KeyValuePair<int,ushort>[2,2]).

Common situations: Dumping glyph mappings into an array that was declared rectangular/multi-dimensional for convenience, or passing a jagged container typed as Array with rank > 1.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontFaceLayoutInfo.cs:720

            }

            public void Clear()
            {
                throw new NotSupportedException();
            }

            public bool Contains(KeyValuePair<int, ushort> item)
            {
                return ContainsKey(item.Key);
            }

            public void CopyTo(KeyValuePair<int, ushort>[] array, int arrayIndex)
            {
                ArgumentNullException.ThrowIfNull(array);

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

                // The extra "arrayIndex >= array.Length" check in because even if _collection.Count
                // is 0 the index is not allowed to be equal or greater than the length
                // (from the MSDN ICollection docs)
                ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(arrayIndex, array.Length);
                ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);

                foreach (KeyValuePair<int, ushort> pair in this)
                    array[arrayIndex++] = pair;
            }

            public int Count
            {
                get { return CMap.Count; }
            }

View on GitHub (pinned to 81131a70a4)