dotnet/wpf · error · ArgumentException

SR.Collection_BadRank

Error message

SR.Collection_BadRank

What it means

TypefaceCollection.CopyTo (Typeface enumeration helper in Fonts.cs) throws this ArgumentException when the destination array has rank other than 1, i.e. it is multi-dimensional. ICollection<T>.CopyTo only supports single-dimensional, zero-based arrays.

Solutions

  1. Use a single-dimensional array: new Typeface[collection.Count].
  2. Flatten multi-dimensional storage into a 1D array before copying.
  3. Or use LINQ ToArray() to allocate the correct shape automatically.

Example fix

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

Strategy: validation

Validate before calling

if (array == null) throw new ArgumentNullException(nameof(array));
if (array.Rank != 1) throw new ArgumentException("array must be single-dimensional");
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));

Type guard

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

Try / catch

try { collection.CopyTo(array, arrayIndex); }
catch (ArgumentException) { /* array was multi-dimensional; allocate 1-D and retry */ }

Prevention

When it happens

Trigger: Calling CopyTo on a Typeface[] allocated as a rectangular/multi-dimensional array (e.g. new Typeface[2,2]) — or via the non-generic ICollection.CopyTo path with such an array.

Common situations: Reusing a 2D buffer for font lists, or legacy code with multi-dimensional arrays passed through reflection/late binding.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Fonts.cs:315

            }

            public bool Contains(Typeface item)
            {
                foreach (Typeface t in this)
                {
                    if (t.Equals(item))
                        return true;
                }
                return false;
            }

            public void CopyTo(Typeface[] 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 (Typeface t in this)
                {
                    array[arrayIndex++] = t;
                }
            }

            public int Count
            {
                get
                {

View on GitHub (pinned to 81131a70a4)