dotnet/wpf · error · ArgumentException
SR.Collection_BadRank
Error message
SR.Collection_BadRank
What it means
CopyTo validates the destination array before copying Typefaces into it. A multi-dimensional array (Rank != 1) cannot be used as an ICollection<T> copy target, so an ArgumentException with resource SR.Collection_BadRank is thrown.
Solutions
- Allocate a one-dimensional Typeface[] whose length is at least the collection count
- Convert any existing multi-dimensional array to a flat 1D array before copying
Example fix
// before var arr = new Typeface[2, 2]; fontFamily.Typefaces.CopyTo(arr, 0); // after var arr = new Typeface[fontFamily.Typefaces.Count]; fontFamily.Typefaces.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("1-D array required", nameof(array));
if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); Prevention
- Always copy ICollection<T> data into freshly allocated 1D arrays
- Remember arrayIndex must be < array.Length even for empty collections
When it happens
Trigger: Calling TypefaceCollection.CopyTo(array, index) where array was created as a multi-dimensional array, e.g. new Typeface[2,2].
Common situations: Passing rectangular arrays produced elsewhere in the code, or generic serialization code that allocates multi-dimensional buffers.
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
- Collection_BadRank
- SR.Collection_BadRank
- SR.Collection_BadRank
- SR.CollectionNumberOfElementsMustBeGreaterThanZero
- SR.CollectionNumberOfElementsMustBeLessOrEqualTo
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/063fb40b1f5de6d6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/TypefaceCollection.cs:61
}
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
{
getView on GitHub (pinned to 81131a70a4)